Comboboxedit并没有Valuemember和displaymember,如何绑定value和Text

添加元素:comboboxedit.Properties.Items.Add();

修改内容 :comboboxEdit.Text = str;

读出选定的数据:string str = comboboxedit.Properties.Items[comboboxeditDept.SelectedIndex].ToString();

在一些情况需要Value 和 Name 进行绑定:

这时得创建一个类:


    
    
  1. public class NameValueClass
  2. {
  3. public string name;
  4. public string id;
  5. public NameValueClass(string _name, string _id)
  6. {
  7. name = _name;
  8. id = _id;
  9. }
  10. public override string ToString()
  11. {
  12. return this.name;
  13. }
  14. }

然后绑定的时候:

comboboxedit.Properties.Items.Add(new NameValueClass(name, value));
    
    

在选定列表某个值的时候就可以读出value了。

 string code = ((NameValueClass)comboboxedit.Properties.Items[comboboxedit.SelectedIndex]).id;
    
    

            </div>

添加元素:comboboxedit.Properties.Items.Add();

修改内容 :comboboxEdit.Text = str;

读出选定的数据:string str = comboboxedit.Properties.Items[comboboxeditDept.SelectedIndex].ToString();

在一些情况需要Value 和 Name 进行绑定:

这时得创建一个类:


  
  
  1. public class NameValueClass
  2. {
  3. public string name;
  4. public string id;
  5. public NameValueClass(string _name, string _id)
  6. {
  7. name = _name;
  8. id = _id;
  9. }
  10. public override string ToString()
  11. {
  12. return this.name;
  13. }
  14. }

然后绑定的时候:

comboboxedit.Properties.Items.Add(new NameValueClass(name, value));
  
  

在选定列表某个值的时候就可以读出value了。

 string code = ((NameValueClass)comboboxedit.Properties.Items[comboboxedit.SelectedIndex]).id;
  
  

            </div>

猜你喜欢

转载自blog.csdn.net/sinat_41746494/article/details/81094471