是否有“显示会员”和“ValueMember”像CheckedListBox控件的属性? C#winforms [英] Is there "DisplayMember" and "ValueMember" like Properties for CheckedListBox control? C# winforms

查看:140
本文介绍了是否有“显示会员”和“ValueMember”像CheckedListBox控件的属性? C#winforms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 DataTable ,具有以下结构:

I have this DataTable with the following structure:

ID | VALUE
----------------
1  | Item 1
2  | Item 2
3  | Item 3

我从 DataTable CheckedListBox 控件中。

And I display the values from the DataTable into a CheckedListBox control by adding each row as an item.

但是如何添加ID?
是否有DisplayMember和ValueMember像CheckedListBox控件的属性?

But how can I include the ID? Is there "DisplayMember" and "ValueMember" like Properties for CheckedListBox control?

推荐答案

是的, code> DisplayMember 和 ValueMember 属性在 CheckedListBox ,虽然文档< a href =http://msdn.microsoft.com/en-us/library/yx9dzztb(v=VS.90).aspx =noreferrer> ValueMember 声称它与这个课程无关。

Well yes, there are DisplayMember and ValueMember properties on CheckedListBox, although the docs for ValueMember claim it's "not relevant to this class".

这是一个快速的例子,显示 DisplayMember working :

Here's a quick example showing DisplayMember working:

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);
    }
}

另请注意,文档状态:


您无法将数据绑定到CheckedListBox。改为使用ComboBox或ListBox。
有关详细信息,请参阅如何:将Windows Forms ComboBox或ListBox控件绑定到数据。

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

Given the above code which works, presumably it's talking about more advanced data binding, using DataSource?

这篇关于是否有“显示会员”和“ValueMember”像CheckedListBox控件的属性? C#winforms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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