简体   繁体   English

ComboBox.ValueMember和DisplayMember

[英]ComboBox.ValueMember and DisplayMember

How do i set this values? 我该如何设置这个值? I have a DataTable with all the data i want to set in the combobox, but i cant find how to set it. 我有一个DataTable,包含我想在组合框中设置的所有数据,但我无法找到如何设置它。

I tried 我试过了

ComboBox1.DataSource = dataTable;
ComboBox1.ValueMember = "id"; // --> once hes here, he just jumps out the method
ComboBox1.DisplayMember = "name";

No compilation error, warning, nothing.. just jumps out! 没有编译错误,警告,没有..只是跳出来!

This is the query to fill the DataTable 这是填充DataTable的查询

"Select * from \"Table\""

I checked with the debugger and the datatable was filled. 我检查了调试器并填充了数据表。 The columns names are "id" and "name". 列名称是“id”和“name”。 ComboBox is blank. ComboBox是空白的。 I'm filling it for the first time! 我第一次填写它!

You should not set datasource of your listbox and/or combobox in this order 您不应按此顺序设置列表框和/或组合框的datasource

ComboBox1.DataSource = dataTable;

ComboBox1.ValueMember = "id"; 

ComboBox1.DisplayMember = "name";

Instead, this is correct order: 相反,这是正确的顺序:

ComboBox1.ValueMember = "id"; 

ComboBox1.DisplayMember = "name";

ComboBox1.DataSource = dataTable;

NOTE: setting datasource should be last line. 注意:设置datasource应该是最后一行。

If you set datasource first, SelectedIndexChanged event will fire and you may get the cast error or other exception. 如果先设置datasource ,则会触发SelectedIndexChanged事件,您可能会收到强制转换错误或其他异常。

  ComboBox1.DataSource= dt; //the data table which contains data
  ComboBox1.ValueMember = "id";   // column name which you want in SelectedValue
  ComboBox1.DisplayMember = "name"; // column name that you need to display as text

They take strings... 他们采取串...

ComboBox1.ValueMember = "id"; 
ComboBox1.DisplayMember = "name";
public class ComboDeger {
    private string yazi;
    private int deger;
    public ComboDeger(string stryazi, int strdeger) {
        this.yazi = stryazi;
        this.deger = strdeger;
    }
    public string yazisi {
        get {
            return yazi;
        }
    }
    public int degeri {
        get {
            return deger;
        }
    }
}
private void combobox_doldur() {
    ArrayList ComboDegerleri = new ArrayList();
    ComboDegerleri.Add(new ComboDeger("9 : NORMAL", 9));
    ComboDegerleri.Add(new ComboDeger("10 : ENGELLİ", 10));
    comboBox1.DataSource = ComboDegerleri;
    comboBox1.DisplayMember = "yazisi";
    comboBox1.ValueMember = "degeri";
}
private void Form3_Load(object sender, EventArgs e) {
    con.Open();
    combobox_doldur();

    // Populate the COMBOBOX using an array as DataSource.
}

I had the same trouble. 我遇到了同样的麻烦。 In my case, SelectedIndexChanged event fires and just jumps out the method. 在我的例子中,SelectedIndexChanged事件触发并且只是跳出该方法。 Try do not use SelectedIndexChanged event. 尝试不要使用SelectedIndexChanged事件。 Or something like this: 或类似的东西:

ComboBox1.SelectedIndexChanged -= new System.EventHandler(ComboBox1_SelectedIndexChanged); 
ComboBox1.DataSource = dataTable; 
ComboBox1.ValueMember = "id";  
ComboBox1.DisplayMember = "name";
ComboBox1.SelectedIndexChanged += new System.EventHandler(ComboBox1_SelectedIndexChanged);

It worked for me. 它对我有用。 =) =)

Using keyvalue pairs to populate a combobox 使用keyvalue对填充组合框

A neat way to populate combo boxes is to set the datasource to a list of keyvalue pairs. 填充组合框的一种巧妙方法是将数据源设置为键值对列表。 It may also inspire using data stored in a list of some kind: 它还可能激发使用存储在某种列表中的数据:

//Some values to show in combobox
string[] ports= new string[3] {"COM1", "COM2", "COM3"};

//Set datasource to string array converted to list of keyvaluepairs
combobox.Datasource = ports.Select(p => new KeyValuePair<string, string>(p, p)).ToList();

//Configure the combo control
combobox.DisplayMember = "Key";
combobox.ValueMember = "Value";
combobox.SelectedValue = ports[0];

The datasource can be populated using this syntax as well: 也可以使用以下语法填充数据源:

ports.Select(p => new { Key = p, Value = p }).ToList();

The technicue may be expanded with more property names for multiple column lists. 可以使用多个列列表的更多属性名称扩展该技术。

ComboBox1.ValueMember = dataTable.Columns["id"].ColumnsName;   // column name which the values are not visible 
ComboBox1.DisplayMember = dataTable.Columns ["name"].ColumnsName;
 /* 
       column name that you need to select item by proprity : 
ComboBox1.SelectedItem; 
Or   you can use easly this : 
ComboBox1.Text; 
*/

ComboBox1.DataSource= dataTable; //the data table which contains data 
// and this should be last :)

you could specify like this 你可以像这样指定

  ComboBox1.ValueMember = "id";  
  ComboBox1.DisplayMember = "name"; 

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

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