Click here to Skip to main content
15,886,857 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
How can I Bind a checkedListBox to DataTable?
For example for Link a ComboBox to DataTable, the following codes are exist:
C#
SqlConnection_11 = new SqlConnection(strcon_11);
SqlDataAdapter_11 = new SqlDataAdapter(SQL_String, SqlConnection_11);
DataSet DataSet11 = new DataSet();
SqlDataAdapter_11.Fill(DataSet11, "my_table");
BindingSource bs1 = new BindingSource(DataSet11, "my_ table ");
ComboBox.DataSource = bs1;
ComboBox.DisplayMember = "Col1";
ComboBox.ValueMember = "Col2";

But for checkedListBox, it dont possible. Because we cant write this line:
C#
checkedListBox.DataSource = bs1;

How can I do this?

Thanks very much
Posted
Updated 20-Oct-13 3:24am
v2

Quote:
But for checkedListBox, it dont possible. Because we cant write this line:

checkedListBox.DataSource = bs1;

How can I do this?
The DataSource[^], DisplayMember[^], ValueMember[^] properties exist on the CheckedListBox control, but they are marked with the BrowsableAttribute(false). Therefore they are hidden from VS's intellisense, but you can still enter these properties.

I think that they were hidden due to this old bug[^], but I can not recreate the bug described in the article.

This works:
DataTable dt = new DataTable("mytable");

dt.Columns.Add("listitem", typeof(string));
dt.Columns.Add("something", typeof(Int32));
dt.Rows.Add(new object[] {"1", 2});
dt.Rows.Add(new object[] {"2", 23});

DataSet ds = new DataSet();
ds.Tables.Add(dt);
BindingSource bs = new BindingSource();
bs.DataSource = ds;
bs.DataMember = "mytable";

checkedListBox1.DataSource = bs;
checkedListBox1.DisplayMember = "listitem";
 
Share this answer
 
Try doing like given below -

C#
SqlConnection_11 = new SqlConnection(strcon_11);
SqlDataAdapter_11 = new SqlDataAdapter(SQL_String, SqlConnection_11);
DataTable dtFill= new DataTable();
SqlDataAdapter_11.Fill(dtFill);
ComboBox.DataSource = dtFill;
ComboBox.DisplayMember = "Col1";
ComboBox.ValueMember = "Col2";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900