手动写入一个combobox控件:
comboBox1.Items.Insert  (0,"请选择");
comboBox1.Items.Insert  (1,"根目录");
comboBox1.SelectedIndex=0;我想取出该控件的SelectedValue,可是提示末将对象引用设置到对象的实例,这说明写入的时候没有指定SelectedValue,那么该怎么样指定呢?多谢了

解决方案 »

  1.   

    存到变量内,控件不认value如:
    ArrayList cbValues = new ArrayList();//定义到外面private ArrayList cbValues = new ArrayList();
                this.comboBox1.Items.Insert(0, "");
                cbValues.Insert(0, value0);
    //取Value的话就去cbValues取
      

  2.   

    不会吧,如果是从数据库绑定的话,我们可以这样设置:
    string sql1="select id,name from addressmanage where type='2'";
    DataTable dt1=DB.DataAccess.GetSqlData (sql1);
    comboBox2.ValueMember = "id";
    ...........取的时候直接取comboBox2.SelectedValue就行了如果不是从数据库绑定的话,是手写的话,就不能用comboBox2.ValueMember这样的方式设置了吗?
      

  3.   

    可以用1楼的办法.从数据取设置ValueMember ...那个是可以的.
      

  4.   

    SelectedValue 属性  获取或设置由 ValueMember 属性指定的成员属性的值。 
    看看MSDN  
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  5.   

                            DataTable dt = new DataTable() ;
    [align=left]dt.Columns.Add("name",typeof(string)) ;
    dt.Columns.Add("id",typeof(int)) ; DataRow row ; row = dt.NewRow() ;
    row["id"] = (int)ZhiLingDanType.TuoYunJianBaoZhuangDan ;
    row["name"] = "托运件包装单" ;
    dt.Rows.Add(row) ; row = dt.NewRow() ;
    row["id"] = (int)ZhiLingDanType.PaiSongChuKuDan ;
    row["name"] = "派送出库单" ;
    dt.Rows.Add(row) ;
    this.DataSource = dt ;
    this.DisplayMember = "name" ;
    this.ValueMember = "id" ; this.SelectedValue = selectedID ;
      

  6.   

    SelectedValue 需要额外分配的,不是初始就有的
      

  7.   

    {            
                comboBox1.Items.Insert(0, new ComboBoxItem("0","请选择"));
                comboBox1.Items.Insert(1, new ComboBoxItem("1","根目录"));
                comboBox1.SelectedIndex = 1;            MessageBox.Show(comboBox1.SelectedItem.ToString());//根目录
                MessageBox.Show(((ComboBoxItem)comboBox1.SelectedItem).Value);//1}  internal class ComboBoxItem:Object
            {
                private string value;
                private string text;
                public ComboBoxItem(string _value, string _text)
                {
                    value = _value;
                    text = _text;
                }            public string Text
                {
                    get { return text; }
                }            public string Value
                {
                    get { return value; }
                }            public override string ToString()
                {
                    return text;
                }        }