问题描述
Hi
如何将checkedListBox绑定到DataTable?
例如,将ComboBox链接到DataTable,存在以下代码:
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;
但是对于checkedListBox,它不可能。因为我们不能写这一行:
checkedListBox.DataSource = bs1;
我该怎么做?
非常感谢
[], [], []属性,但它们标有BrowsableAttribute(false)。因此,它们隐藏在VS的智能感知中,但您仍然可以输入这些属性。
我认为它们因 [],但我无法重新创建文章中描述的错误。
这有效:
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;
Hi
How can I Bind a checkedListBox to DataTable?
For example for Link a ComboBox to DataTable, the following codes are exist:
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:
checkedListBox.DataSource = bs1;
How can I do this?
Thanks very much
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";
这篇关于没有用于checkedListBox的DataSource绑定到DataTable(在c#中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!