如何将checkedListBox链接到DataTable? [英] How can I Link a checkedListBox to DataTable?

查看:118
本文介绍了如何将checkedListBox链接到DataTable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将checkedListBox链接到DataTable(在c#中)







你好



如何将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; 







我该怎么做?







非常感谢很多

解决方案

你可以添加如下项目



Quote:

foreach(dt.Rows中的DataRow项目)

{

MyCheckedListBox.Items.Add(item [Name] ] .ToString(),Convert.ToBoolean(item [Checked]));

}





或试试类似下面的东西



Quote:

((ListBox)MyCheckBoxList).DataSource = dt;

((ListBox)MyCheckBoxList).DisplayMember =name;

((ListBox)MyCheckBoxList).ValueMember =enabled;


Link a checkedListBox to DataTable(in c#)


Hi
hello

How can I Link 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

解决方案

You can either add items like below

Quote:

foreach (DataRow item in dt.Rows)
{
MyCheckedListBox.Items.Add(item["Name"].ToString(), Convert.ToBoolean(item["Checked"]));
}



Or try something like below

Quote:

((ListBox)MyCheckBoxList).DataSource = dt;
((ListBox)MyCheckBoxList).DisplayMember = "name";
((ListBox)MyCheckBoxList).ValueMember = "enabled";


这篇关于如何将checkedListBox链接到DataTable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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