简体   繁体   English

多列组合框

[英]Combo Boxes With Multiple Columns

I've read that combo boxes cannot have multiple columns. 我读过组合框不能有多列。 Which leaves me a bit stuck since what I want to do is display one field from a table, but return the corresponding value from a second field in the same table ie 这让我有些困惑,因为我想做的是显示表中的一个字段,但是从同一表中的第二个字段返回相应的值,即

I'd like to show CustomerNames in the combo box, but when the user selects a name, the CustomerID field is returned instead. 我想在组合框中显示CustomerNames,但是当用户选择一个名称时,将返回CustomerID字段。 Whats the best work around for this? 最好的解决方法是什么?

Best way is to use ComboBoxes DisplayMember and ValueMember properties 最好的方法是使用ComboBoxes DisplayMemberValueMember属性

set the ComboBox.DisplayMember to the property you want to display. ComboBox.DisplayMember设置为要显示的属性。 Use ValueMember for property you want to return. 对要返回的属性使用ValueMember Then you can use the ComboBox.SelectedValue to get the current/selected ValueMember 然后,您可以使用ComboBox.SelectedValue获取当前/选定的ValueMember

ComboBoxItem的值不必与Text相同,请考虑使用ID作为值。

You don't need multiple columns to implement it. 您不需要多个列即可实现它。

class Member
{
    public string Name{get;set;}
    public string Address{get;set;}
    public int ID{get;set;}

    public string Description
    {
        get
        {
            return string.Format("{0}, {1}", Name, Address);
        }
    }
}

var members = new []
{
   new Member(){ID = 1, Name = "John", Address = "Addr 1"},
   new Member(){ID = 2, Name = "Mary", Address = "Addr 2"}
};

m_ComboBox.DataSource = members;
m_ComboBox.DisplayMember = "Description"
m_ComboBox.ValueMember = "ID";

now you can access seleceted ID 现在您可以访问选择的ID

var selectedID = m_ComboBox.CelectedValue();

您可以通过将ComboBox的DisplayMemberValueMember属性分别设置为"CustomerName""CustomerID"来实现所需的行为。

Take a look at the ValueMember property. 看看在ValueMember属性。 You should set this to CustomerID. 您应该将此设置为CustomerID。 After you bind to your combobox, the actual field displayed to the user will be the CustomerName, but when you want to get the value of 'CustomerName', it will return the CustomerID. 当您绑定到你的组合框,实际的现场显示给用户的将是客户名称,但如果你想获得“客户名称”的值,它会返回客户ID。

When you want to get the value of the combobox, simply reference the SelectedValue . 当你想要得到的组合框的值,只需引用SelectedValue

If you are adamant about displaying both of these in the combobox, there are some hackish ways of doing this but I would recommend reviewing your requirements again and seeing if it is absolutely necessary. 如果你是坚定的关于在下拉列表中显示这两种,也有这样的一些hackish的方式,但我会建议重新审查您的要求,看它是否是绝对必要的。

Alternatively, you can define KeyValuePair objects with your intended Ids and text fields. 或者,您可以使用预期的ID和文本字段定义KeyValuePair对象。 Feed them to the combo, since its Items property is a collection of objects. 将它们提供给组合,因为它的Items属性是对象的集合。 Then, for retrieval use a cast like 然后,要进行检索,请使用

var x = (KeyValuePair)combo.Items[0];

and then acces then Key and Value properties of x. 然后访问x的键和值属性。

您可以使用ComboBoxItem的Tag属性存储值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM