没有用于checkedListBox的DataSource绑定到DataTable(在c#中) [英] No exist DataSource for a checkedListBox to Bind to DataTable (in c#)

查看:375
本文介绍了没有用于checkedListBox的DataSource绑定到DataTable(在c#中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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; 



我该怎么做?



非常感谢

解决方案

尝试如下所示 -



 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;


Quote:

但对于checkedListBox,它不可能。因为我们不能写这行:



checkedListBox.DataSource = bs1;



我该怎么做?

DataSource [ ^ ], DisplayMember [ ^ ], ValueMember [ ^ ]属性,但它们标有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

解决方案

Try doing like given below -

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";


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";


这篇关于没有用于checkedListBox的DataSource绑定到DataTable(在c#中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆