将默认值添加到ComboBox

时间:2013-11-27 21:05:14

标签: c# .net-2.0

我有一个使用ComboBox的Windows应用程序,之前手动填写了“编辑项目”。我正在更改它以从Database table获取数据。

如何添加空值以便他们必须选择一个选项?

目前它会自动显示dataset comboBox中返回的第一个人,我想知道我是如何强制它显示空白值作为默认值?

Designer生成的代码。

    // attorneySelectDropDown
    // 
    this.attorneySelectDropDown.DataSource = this.jMeFileAssignAttorneyBindingSource;
    this.attorneySelectDropDown.DisplayMember = "name";
    this.attorneySelectDropDown.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    this.attorneySelectDropDown.FormattingEnabled = true;
    this.attorneySelectDropDown.Location = new System.Drawing.Point(13, 15);
    this.attorneySelectDropDown.Name = "attorneySelectDropDown";
    this.attorneySelectDropDown.Size = new System.Drawing.Size(298, 21);
    this.attorneySelectDropDown.TabIndex = 4;
    this.attorneySelectDropDown.ValueMember = "name";
    this.attorneySelectDropDown.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);

1 个答案:

答案 0 :(得分:8)

Fill方法之后,您应该将SelectedItem属性设置为null:

private void Form1_Load(object sender, EventArgs e)
{           
    // TODO: This line of code loads data into the 'stackoverflowDataSet.Person' table. You can move, or remove it, as needed.
    this.personTableAdapter.Fill(this.stackoverflowDataSet.Person);
    comboBox1.SelectedItem = null;
}

在这种情况下,用户必须从ComboBox中选择项目。