private void button1_Click(object sender, EventArgs e)
{
    ////这段可正常运行
    //comboBox1.DisplayMember = "CD";
    //comboBox1.ValueMember = "CD";
    //comboBox1.DataSource = ftTestClass.GetDataTable_Type();
    ////comboBox1.SelectedIndex = 2;
    //comboBox1.SelectedValue = "BB";    //这段出错
    using (ComboBox cmb = new ComboBox())
    {
        //cmb.Parent = this;
        cmb.ValueMember = "CD"; //Code
        cmb.DisplayMember = "CD"; //Code
        cmb.DataSource = ftTestClass.GetDataTable_Type(); //{AA, BB, CC, DD, EE}
        //cmb.SelectedIndex = 2;
        cmb.SelectedValue = "BB";
        Console.WriteLine(cmb.SelectedValue.ToString()); //出错,因为 SelectedValue=null,明明付了值呀,付不上去
    }
}

解决方案 »

  1.   

    cmb的DataSource中存在“BB”值吗?
      

  2.   

    cmb.SelectedValue = "BB";
    以后下面的代码不要写了,在页面上当前选择项是这个吗?
      

  3.   

    DataSource中CD字段中不存在"BB"值
      

  4.   

    检查 ftTestClass.GetDataTable_Type(); 查到的数据~
      

  5.   

    默认的ValueMember,SelectedValue,DisplayMember不够灵活
    class Grade{
      public string Text="";
      public int =100;
      public override string ToString(){
      return Text;
      }
    }
    在ComboBox中添加此类的对象就行了,想怎么玩都行
      

  6.   

    赋值?
    不是这样嘛
      ComboBox cmb = new ComboBox();
                cmb.Items.Add("a");
    你那是什么赋值?
    Combobox里面都是 items,每个item对应一个object
    就可以了。
    不明白你要怎么操作
    ComboBox ,ComboBox ,就是组合box,赋值只能对这个组合里面的单个对象 来操作啊。
      

  7.   

    代码全在这里,在vs中(我装的是vs2008)试一下,这段会出错,其值付不上。
    public static DataTable GetDataTable_Type()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("CD", typeof(string));
        dt.Columns.Add("DS", typeof(string));
        dt.Columns.Add("CDDS", typeof(string));
        dt.LoadDataRow(new string[] { "AA", "aaa", "AA - aaa" }, true);
        dt.LoadDataRow(new string[] { "BB", "bbb", "BB - bbb" }, true);
        dt.LoadDataRow(new string[] { "CC", "ccc", "CC - ccc" }, true);
        dt.LoadDataRow(new string[] { "DD", "ddd", "DD - ddd" }, true);
        dt.LoadDataRow(new string[] { "EE", "eee", "EE - eee" }, true);
        return dt;
    }private void button1_Click(object sender, EventArgs e)
    {
        //这段出错
        using (ComboBox cmb = new ComboBox())
        {
            //cmb.Parent = this;
            cmb.ValueMember = "CD"; //Code
            cmb.DisplayMember = "CD"; //Code
            cmb.DataSource = this.GetDataTable_Type(); //{AA, BB, CC, DD, EE}
            //cmb.SelectedIndex = 2;
            cmb.SelectedValue = "CC";
            Console.WriteLine(cmb.SelectedValue.ToString()); //出错,因为 SelectedValue=null,明明付了值呀,付不上去
        }
    }
      

  8.   

    我用的是数据源绑定,数据源中有要付的项"AA,BB,CC,DD"。
    付值不会提示出错,但在调试器中看到其值仍为null,所以不能ToString。
    (调试时把 上面代码中的 public static DataTable GetDataTable_Type()的“static”去掉,笔误)
      

  9.   

    你这个我不知道什么地方出错了,但是你如果cmb.SelectedValue = "CC",那么就必须在上面的集合中存在,这样就行,我做了个测试,直接Console.WriteLine(cmb.SelectedValue.ToString())就会出现SelectedValue 为null,不知道为什么,但是你把这个改为cmb.DisplayMember.ToString()就可以,我也很想知道这个Console.WriteLine(cmb.SelectedValue.ToString())就会出现SelectedValue 为null是什么原因。
      

  10.   

    comboBox.selectValue只能赋值成null和"";
    你不管赋什么值都会变成null;你是想设置cmb的默认选项吧
    只能用selectIndex来设定
      

  11.   

    我想在datagridview控件上浮个ComboBox控件,
    因为DataGridViewComboBoxColumn不好看,
    但自己写的付不了值。
    public DataTable GetDataTable_Type()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("CD", typeof(string));
        dt.Columns.Add("DS", typeof(string));
        dt.Columns.Add("CDDS", typeof(string), "CD+' - '+DS");
        dt.LoadDataRow(new string[] { "AA", "aaa" }, true);
        dt.LoadDataRow(new string[] { "BB", "bbb" }, true);
        dt.LoadDataRow(new string[] { "CC", "ccc" }, true);
        dt.LoadDataRow(new string[] { "DD", "ddd" }, true);
        dt.LoadDataRow(new string[] { "EE", "eee" }, true);
        return dt;
    }protected override void OnCellEnter(DataGridViewCellEventArgs e) //DataGridView event
    {
        //下拉列表
        if (ftListColumns != null)
        {
            if (ftListColumns.Contains(this.CurrentCell.OwningColumn))
            {
                if (this.CurrentCell.OwningColumn.DataPropertyName == "TP")
                {
                    ComboBox cmb = new ComboBox();
    //cmb.Visible = false;
    //cmb.Parent = this;
                    cmb.DropDownWidth = 200;
                    cmb.DropDownStyle = ComboBoxStyle.DropDownList;
                    DataTable dt = ftTestClass.GetDataTable_Type();
                    cmb.DisplayMember = "CDDS";
                    cmb.ValueMember = "CD";
                    cmb.DataSource = dt;
                    cmb.SelectedValue = this.CurrentCell.EditedFormattedValue; //这里付的是网格当前值,但付不上,仍是null
                    //cmb.value = this.CurrentCell.EditedFormattedValue;
                    //cmb.Text = this.CurrentCell.EditedFormattedValue.ToString();
                    cmb.SelectedValueChanged += new EventHandler(delegate(object sender2, EventArgs e2)
                    {
                        if (!this.CurrentCell.Value.Equals((sender2 as ComboBox).SelectedValue))
                        {
                            this.CurrentCell.Value = (sender2 as ComboBox).SelectedValue; //
                        }
                    });
    //把combobox浮到DataGridView上面
    {
    Control ctl = cmb;
                this.Controls.Add(ctl);

                ctl.Bounds = this.GetCellDisplayRectangle(this.CurrentCell.ColumnIndex, this.CurrentCell.RowIndex, true);
                ctl.LostFocus += new EventHandler(delegate(object sender, EventArgs e) { (sender as Control).Visible = false; });
                ctl.Visible = true;
                ctl.Focus();
                ctl.BringToFront();
    }
                }
            }
        }
        base.OnCellEnter(e);
    }protected override void OnCellLeave(DataGridViewCellEventArgs e)
    {
        base.OnCellLeave(e); if (this.ftFloatControl != null)
        {
           this.ftFloatControl.Visible = false;
        }
    }
      

  12.   

    自己也顶一个。
    一直很烦combobox,不管是delphi、VB6还是C#中的都烦。
    唯独delphi中的DbLookupCombobox很好用。
      

  13.   

    楼主这样的问题我也碰到过,你可以在winform界面中看下,“BB”有没有被选中,其实好象是选不中的,用SelectIndex可以选
      

  14.   

    在界面中的Combobox是不会出这问题的,只有新new出来的才会有这现象。
    看 0楼 的代码就知道了。
      

  15.   

    cmb.SelectedValue = "CC";
    可能是只读的属性吧,不能赋值
      

  16.   

    人家考算法之类的,我就出这题:“上机调试,让 7楼 的代码能够正确执行”。
    连Combobox控件都不会用,,
      

  17.   

    发现只要:
    1. 设置了 ComboBox的Parent 属性 
    2. 或者 DataGridView1.Controls.Add(cmb)
    就可以 付 SelectedValue 的值。难道保存 SelectedValue还要依lai4 其Parent?
      

  18.   

        using (ComboBox cmb = new ComboBox())
        {
         
        }这句话是什么意思知道吗?  出了大括号ComboBox就会被释放掉的呢.....
      

  19.   

    这是测试时这么写的,问题关键是还没出using的地界就throw出了异常。
      

  20.   

    select....是你选择的东西,你必须有选择的对象啊。
      

  21.   

    dropdownstyle:dropdownlist就不会这个问题了