James Wilson
James Wilson

Reputation: 5150

Adding default value to ComboBox

I have a windows application that is using a ComboBox which previously had the 'Edit Items' filled out by hand. I am changing this to grab the data from a Database table.

How do I add a blank value so they must selected an option?

Currently it automatically shows the first person returned from the dataset in the comboBox I'm wondering how I force it to show a blank value as the default?

Designer generated code.

    // 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);

Upvotes: 1

Views: 11208

Answers (1)

kmatyaszek
kmatyaszek

Reputation: 19296

After Fill method, you should set SelectedItem property to 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;
}

In this case user have to select item from ComboBox.

Upvotes: 9

Related Questions