ComboBox.ValueMember and DisplayMember

后端 未结 8 1553
野的像风
野的像风 2020-12-06 12:45

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.

I tried

ComboBox1.DataSo         


        
相关标签:
8条回答
  • 2020-12-06 13:15

    They take strings...

    ComboBox1.ValueMember = "id"; 
    ComboBox1.DisplayMember = "name";
    
    0 讨论(0)
  • 2020-12-06 13:15

    you could specify like this

      ComboBox1.ValueMember = "id";  
      ComboBox1.DisplayMember = "name"; 
    
    0 讨论(0)
  • 2020-12-06 13:17

    I had the same trouble. In my case, SelectedIndexChanged event fires and just jumps out the method. Try do not use SelectedIndexChanged event. 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. =)

    0 讨论(0)
  • 2020-12-06 13:24

    You should not set datasource of your listbox and/or combobox in this order

    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.

    If you set datasource first, SelectedIndexChanged event will fire and you may get the cast error or other exception.

    0 讨论(0)
  • 2020-12-06 13:26
    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.
    }
    
    0 讨论(0)
  • 2020-12-06 13:27

    Using keyvalue pairs to populate a combobox

    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.

    0 讨论(0)
提交回复
热议问题