加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

有“DisplayMember”和“ValueMember”像CheckedListBox控件的属

发布时间:2020-12-16 05:30:00 所属栏目:百科 来源:网络整理
导读:我有这个DataTable具有以下结构: ID | VALUE----------------1 | Item 12 | Item 23 | Item 3 并且通过将每行作为项目添加,我将DataTable中的值显示为CheckedListBox控件. 但是如何包含ID? 有“DisplayMember”和“ValueMember”像CheckedListBox控件的属
我有这个DataTable具有以下结构:
ID | VALUE
----------------
1  | Item 1
2  | Item 2
3  | Item 3

并且通过将每行作为项目添加,我将DataTable中的值显示为CheckedListBox控件.

但是如何包含ID?
有“DisplayMember”和“ValueMember”像CheckedListBox控件的属性?

解决方法

那么是的,CheckedListBox上有DisplayMember和ValueMember属性,尽管 ValueMember的文档声称它与这个类无关.

以下是显示DisplayMember工作的快速示例:

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来讨论更高级的数据绑定?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读