最近刚刚从ASP转过来研究.net(c#),先搞一个简单的东东看看,遇到以下问题:
combobox相关代码//定义一个类
public class Dropselect
        {
            public Dropselect(int id, string skey, string title)
            {
                this.Id=id;
                this.Skey=skey;
                this.Title=title;
            }
            public int Id;
            public string Skey;
            public string Title;
        }//将选项添加进去
this.comboBox5.Items.AddRange(new object[] {
                new Dropselect(0,"xx","-条件1-"),
                new Dropselect(1,"FBillNo","订单号"),
                new Dropselect(2,"FDeptId","部  门"),
                new Dropselect(3,"FEmpId","业务员"),
                new Dropselect(4,"FModel","型  号"),
                new Dropselect(5,"FItemId","品  牌"),
                new Dropselect(6,"FCustId","客  户")});
            this.comboBox5.DisplayMember = "Title";
            this.comboBox5.ValueMember = "Skey";VS没有报错,可运行出来显示的是"WindowsFormsApplication1.MainFrame.Dropselect"
不知道怎么回事?

解决方案 »

  1.   

    办法1:
    public class Dropselect
      {
       public override string ToString()
       {
           return Title;
       }

      public Dropselect(int id, string skey, string title)
      {
      this.Id=id;
      this.Skey=skey;
      this.Title=title;
      }
      public int Id;
      public string Skey;
      public string Title;
      }
    办法二
    public class Dropselect
      {
      public Dropselect(int id, string skey, string title)
      {
      this.Id=id;
      this.Skey=skey;
      this.Title=title;
      }
      public int Id { get; set; }
      public string Skey { get; set; }
      public string Title { get; set; }
      }
      

  2.   

    谢谢,再问一下,我如何获取对应的值呢?现在获取到的都是TEXT
      

  3.   

    呵呵,我指的是调用值的方法
    例如:combobox.text.tostring()这是返回显示的项
      

  4.   

    string s = combobox1.SelectedValue;
      

  5.   

    提示无法将类型"object"隐式转换为"string"
      

  6.   

    string s = combobox1.SelectedValue.ToString();
      

  7.   


    string s = combobox1.SelectedValue.tostring();
      

  8.   


    还是不行,我用Messagebox.show(combobox1.SelectedValue.Tostring());提示未将对象引用设置到对象的实例
      

  9.   

    但是我Messagebox.show(combobox1.Text.Tostring());都是正确的
      

  10.   

    代码不是凑出来的。很怀疑你这基本语法都没熟悉的以前怎么写asp.net程序的如果你会下一个断点调试的话,你可以试试看,如果这都不会。我也没办法了。
      

  11.   


    调试了一下,还是string s = combobox1.SelectedValue.ToString();这句异常,会不会前面ValueMember没有数值?