我有这个DataTable具有以下结构:
ID | VALUE ---------------- 1 | Item 1 2 | Item 2 3 | Item 3
并且通过将每行作为项目添加,我将DataTable中的值显示为CheckedListBox控件.
但是如何包含ID?
有“DisplayMember”和“ValueMember”像CheckedListBox控件的属性?
解决方法
那么是的,CheckedListBox上有DisplayMember和ValueMember属性,尽管
ValueMember
的文档声称它与这个类无关.
using System; using System.Drawing; using System.Windows.Forms; class Test { static void Main() { CheckedListBox clb = new CheckedListBox { DisplayMember = "Foo",ValueMember = "Bar",Items = { new { Foo = "Hello",Bar = 10 },new { Foo = "There",Bar = 20 } } }; Form f = new Form { Controls = { clb } }; Application.Run(f); } }
另请注意,文档状态:
You cannot bind data to a CheckedListBox. Use a ComboBox or a ListBox for this instead.
For more information,see How to: Bind a Windows Forms ComboBox or ListBox Control to Data.
鉴于上述代码的作用,大概是使用DataSource来讨论更高级的数据绑定?