微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有“DisplayMember”和“ValueMember”像CheckedListBox控件的属性? C#winforms

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

原文地址:https://www.jb51.cc/c/114600.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐