在winform中
this.comboBox1.DataSource = ds1.Tables[0];
this.comboBox1.DisplayMember = "Name";
 this.comboBox1.ValueMember = "Num";
数据是用以上方法绑定的,
我现在是想遍历得到每项对应的num,不是Name,现在不用dataTable,能否遍历得到每一项对应的关联数据num
对选中项来说,得到其value的方法是comboBox.SelcetedValue,得到每一项如何操作

解决方案 »

  1.   

    好像要那样也只能循环整到text,值不好搞额。。  string values;            for (int i = 0; i < cmbScaleMode.Items.Count; i++)
                {
                    values = cmbScaleMode.items[i].Tostring;
                }整到了text..弄过来的来吧。。
      

  2.   


    这个你要自己写一个对象来存储Text和Value,因为comboxBOx1.Items里面是object对象public class ComboBoxItem
    {
      private string _value;
      private string _text;  public string Value { get { return _value; } }
      public string Text  { get { return _text ; } }  public ComboBoxItem (string cValue, cText)
      {
        this._value = cValue;
        this._text  = cText;
      }
    }
    ComboBox myBox = new ComboBox ();
    myBox.Items.Add (new ComboBoxItem ("1", "January"));
    myBox.Items.Add (new ComboBoxItem ("2", "February"));for(int i=0;i<this.comboxBOx1.Items.Count;i++)
    {
        ComboBoxItem item=comboxBOx1.Items[i] as ComboBoxItem;
        string getValue=item.Value;
    }
      

  3.   

    DataTable dt = new DataTable();
                dt.Columns.Add("id");
                dt.Columns.Add("name");
                DataRow dr = dt.NewRow();
                dr[0] = "1000";
                dr[1] = "测试1000";
                dt.Rows.Add(dr);
                dr = dt.NewRow();
                dr[0] = "1001";
                dr[1] = "测试1002";
                dt.Rows.Add(dr);            comboBox1.DataSource = dt;
                comboBox1.DisplayMember = "name";
                comboBox1.ValueMember = "id";
                comboBox1.Refresh();            foreach (Object oDic1 in comboBox1.Items)
                {
                    DataRowView dr1 = (DataRowView)oDic1;
                    MessageBox.Show(dr1["id"].ToString() + "_______" + dr1["name"].ToString());
                }