comboBox dropdownlist droplist 添加 value

  1. public class ListItem   
  2. {   
  3.     private string _key = string.Empty;   
  4.     private string _value = string.Empty;   
  5.     public ListItem(string pKey, string pValue)   
  6.     {   
  7.         _key = pKey;   
  8.         _value = pValue;   
  9.     }   
  10.     public override string ToString()   
  11.     {   
  12.         return this._value;   
  13.     }   
  14.     public string Key   
  15.     {   
  16.         get  
  17.         {   
  18.             return this._key;   
  19.         }   
  20.         set  
  21.         {   
  22.             this._key = value;   
  23.         }   
  24.     }   
  25.     public string Value   
  26.     {   
  27.         get  
  28.         {   
  29.             return this._value;   
  30.         }   
  31.         set  
  32.         {   
  33.             this._value = value;   
  34.         }   
  35.     }   
  36. }   
  37.   
  38.   
  39. ///////////////////////////////////////////////////////////////   
  40. comboBox1.Items.Add(new ListItem("请选择""0"));   
  41. DataView dv = Comm.DB.ExecuteDataSet("select txtName,ID from dept where ParentID=24").Tables[0].DefaultView;   
  42. foreach (DataRowView drv in dv)   
  43. {   
  44.     comboBox1.Items.Add(new ListItem(drv["txtName"].ToString() , drv["ID"].ToString()));   
  45. }   
  46. comboBox1.DisplayMember = "Key";   
  47. comboBox1.ValueMember = "Value";   
  48. comboBox1.SelectedIndex = 0;   
  49.   
  50. ///////////////////////////////////////////////////////////////////   
  51.  MessageBox.Show(((ListItem)comboBox1.SelectedItem).Value); 
原文地址:https://www.cnblogs.com/liufei88866/p/1762647.html