如果ComboBox是非绑定,我们可以直接添加一项到ComboBox的项集合中。
代码:
this.comboBox1.Items.Insert(0, "please select");
this.comboBox1.SelectedIndex = 0;
如果ComboBox是数据绑定的,我们就需要将这一项添加到数据源中。
代码:
Customer cust = new Customer("Joe", null);
DataRow row = statesTable.NewRow();
row["Name"] = " please select";
row["Code"] = DBNull.Value;
statesTable.Rows.Add(row);
this.statesCB.DisplayMember = "Name";
this.statesCB.ValueMember = "Code";
this.statesCB.DataSource = statesTable;
this.statesCB.DataBindings.Add("SelectedValue", cust, "StateID", true);