Tuesday, July 14, 2009

Bind CheckedListBox Directly with Datatable in C#

chkListBuilding.Items.Clear();
DataTable dtBuilding = (new BuildingManager()).SelectBuilding();
((ListBox)chkListBuilding).DataSource = dtBuilding;
((ListBox)chkListBuilding).DisplayMember = "BuildingName";
((ListBox)chkListBuilding).ValueMember = "BuildingID";

2 comments:

Unknown said...
This comment has been removed by the author.
Fernando Cubias said...

For Someone else this:
((ListBox)chkListBuilding).DataSource = dtBuilding;
((ListBox)chkListBuilding).DisplayMember = "BuildingName";
((ListBox)chkListBuilding).ValueMember = "BuildingID";


is not necessary you can use:

chkListBuilding.DataSource = dtBuilding;
chkListBuilding.DisplayMember = "BuildingName";
chkListBuilding.ValueMember = "BuildingID";


You don't need a cast.