我在combobox控件items添加了4个选项是中文的,【编号 名称 数值】;在数据库中对应的列名是【ID,NAME,NUM】,在查询的时候如果获取combobox1.text的话是中文的,与数据库中对应的列名不一样,这个问题该如何解决我是新手。

解决方案 »

  1.   

    // 定义个类
    public class Tuple
    {
        public string name { get; set; }
        public string column { get; set; }
    }// 绑定代码
    var list = new List<Tuple>(3);
    list.Add(new Tuple { name = "编号", column = "ID" });
    list.Add(new Tuple { name = "名称", column = "NAME" });
    list.Add(new Tuple { name = "数值", column = "NUM" });
    this.comboBox1.DataSource = list;
    this.comboBox1.DisplayMember = "name";
    this.comboBox1.ValueMember = "column";// 取值
    var result = this.comboBox1.SelectedValue;
      

  2.   

    可以像avphoenixi那样定义一个类,类里重载ToString,那样添加到combox中会显示ToString的内容
      

  3.   

    var names = new string[] { "编号", "名称", "数值" };
    var columns = new string[] { "ID", "NAME", "NUM" };
    this.comboBox1.DataSource = names;
    this.comboBox1.Tag = columns;var result = ((string[])this.comboBox1.Tag)[this.comboBox1.SelectedIndex];
      

  4.   

    还是没有用对,每次选择后,只是提交过去ID,如果选择名称,也是ID,不是NAME。
      

  5.   

    那您帮我看一下这段代码 是哪里有问题?                var list = new List<Tuple>(3);
                    var names = new string[] { "物料代码", "物料名称", "规格型号", "单价", "属性" };
                    var column = new string[] { "matID", "matName", "model", "price", "attr" };
                    this.comboBox1.DataSource = list;
                    this.comboBox1.DataSource = names;
                    this.comboBox1.Tag = column;                var result = ((string[])this.comboBox1.Tag)[this.comboBox1.SelectedIndex];
                    dataGridView1.DataSource = MyClass.getDataSet("select matID as '物料代码',matName as '物料名称',model as '规格型号',price as '单价',onhandQty as '库存',unit as '单位',Explanation as '产品说明',attr as '属性' FROM material where " + result + " like '%" + textBox1.Text.Trim() + "%'");
      

  6.   

    不管怎么选择,一直是"matID",而且只要点查询,还会报错。
      

  7.   

    // 绑定就这4行
    var names = new string[] { "物料代码", "物料名称", "规格型号", "单价", "属性" };
    var column = new string[] { "matID", "matName", "model", "price", "attr" };
    this.comboBox1.DataSource = names;
    this.comboBox1.Tag = column;// 取值时才执行这行,result 就是结果
    var result = ((string[])this.comboBox1.Tag)[this.comboBox1.SelectedIndex];
      

  8.   

    嗯,我取值以后,在SQL语句中获取,连接到数据库,但是无论combobox怎么选 都是matID
    是matID也就算了,但不知道为什么还报错。。sql语句也没有问题啊。。唉~~~。select matID as '物料代码',matName as '物料名称',model as '规格型号',price as '单价',onhandQty as '库存',unit as '单位',Explanation as '产品说明',attr as '属性' FROM material where matID like '%A%'
    var result = ((string[])this.comboBox1.Tag)[this.comboBox1.SelectedIndex];
                    dataGridView1.DataSource = MyClass.getDataSet("select matID as '物料代码',matName as '物料名称',model as '规格型号',price as '单价',onhandQty as '库存',unit as '单位',Explanation as '产品说明',attr as '属性' FROM material where " + result + " like '%" + textBox1.Text.Trim() + "%'");