日期:2014-05-18  浏览次数:20778 次

请问Winform里怎么手动设置combobox的实际值
例如 显示值为 a,b ,c 实际值为 1,2,3 
 不要求从数据库里取值而是手动添加,应该怎么实现?

------解决方案--------------------
C# code

 DataTable dt = new DataTable();
            dt.Columns.Add("DisplayValue");
            dt.Columns.Add("Value");

            DataRow dr = dt.NewRow();
            dr["DisplayValue"] = "a";
            dr["Value"] = "1";
            dt.Rows.Add(dr);


            dr = dt.NewRow();
            dr["DisplayValue"] = "b";
            dr["Value"] = "2";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["DisplayValue"] = "c";
            dr["Value"] = "3";
            dt.Rows.Add(dr);

            comboBox1.ValueMember = "Value";
            comboBox1.DisplayMember = "DisplayValue";
            comboBox1.DataSource = dt;

------解决方案--------------------
手动初始化:
C# code
string[] str = {"a","b","c"};
this.comboBox1.Items.AddRange(str);
this.comboBox1.SelectedIndex = 0;

------解决方案--------------------
探讨
手动初始化:
C# codestring[] str={"a","b","c"};this.comboBox1.Items.AddRange(str);this.comboBox1.SelectedIndex=0;
取值的时候:
C# codeintvalue=this.comboBox1.SelectedIndex+1;