是否有“DisplayMember”?和“ValueMember”就像 CheckedListBox 控件的属性一样? C# winforms

发布于 2024-09-25 16:03:14 字数 302 浏览 9 评论 0原文

我有一个具有以下结构的 DataTable

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

并且通过将每一行添加为一个项目,将 DataTable 中的值显示到 CheckedListBox 控件中。

但是我怎样才能包含ID呢? 是否有“DisplayMember”和“ValueMember”之类的 CheckedListBox 控件的属性?

I have this DataTable with the following structure:

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

南笙 2024-10-02 16:03:14

是的,CheckedListBox 上有 DisplayMemberValueMember 属性,尽管 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);
    }
}

另请注意,文档指出:

您无法将数据绑定到 CheckedListBox。为此,请使用组合框或列表框。
有关详细信息,请参阅如何:将 Windows 窗体组合框或列表框控件绑定到数据。

鉴于上面的代码有效,大概它正在谈论更高级的数据绑定,使用DataSource

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

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

Also note that the docs state:

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.

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

碍人泪离人颜 2024-10-02 16:03:14

DataSource、DisplayMember 和 ValueMember 属性可用于此控件,但它们不会显示在 IntelliSense 中:MSDN

不过,您应该能够使用它们。

The DataSource, DisplayMember and ValueMember properties are available for this control but they are not displayed in the IntelliSense: MSDN

You should be able to use them though.

笑脸一如从前 2024-10-02 16:03:14

是的,CheckedListBox 中有“显示成员”和“值成员”属性。

您可以像在组合框中一样设置属性:

   public void PopulateListBox(System.Windows.Forms.CheckedListBox lb, string displayMember, string valueMember, DataTable data)
   {
        lb.DataSource = data; // where data is the datatable. datatable filled up with //data fetched from database.
        lb.ValueMember = valueMember;
        lb.DisplayMember = displayMember;
   }

Yes there are 'display member' and 'value member' properties in CheckedListBox.

You can set the properties as you do in combobox:

   public void PopulateListBox(System.Windows.Forms.CheckedListBox lb, string displayMember, string valueMember, DataTable data)
   {
        lb.DataSource = data; // where data is the datatable. datatable filled up with //data fetched from database.
        lb.ValueMember = valueMember;
        lb.DisplayMember = displayMember;
   }
那请放手 2024-10-02 16:03:14

法语文档说:Cette propriété不属于此类的贴花。
“该属性不适用于此类”。
这一小行文字在美国文档中不可见......

The french documentation say : Cette propriété ne s'applique pas à cette classe.
"This property does not apply for this class".
This little line of text is not visible in the us documentation...

梦里泪两行 2024-10-02 16:03:14

我使用的是 Visual Studio 2008。这两个属性“DisplayMember”& “ValueMember”与“DataSource”属性一起存在,但它们不会出现在 Intellisense 中。

I am using Visual Studio 2008. These two properties "DisplayMember" & "ValueMember" are there along with "DataSource" property but they don't apper in Intellisense.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文