Thus, you cannot change Elements when you are bound to a DataSource, then only the option to add an empty string changes your data source. Create an empty object and add it to the data source. For example. if you have a list of some Person entities bound to combobox:
var people = Builder<Person>.CreateListOfSize(10).Build().ToList(); people.Insert(0, new Person { Name = "" }); comboBox1.DisplayMember = "Name"; comboBox1.DataSource = people;
You can define the static property Empty in your class:
public static readonly Person Empty = new Person { Name = "" };
And use it to insert an empty default element:
people.Insert(0, Person.Empty);
This will also check if the selected item is the default:
private void comboBox_SelectedIndexChanged(object sender, EventArgs e) { Person person = (Person)comboBox.SelectedItem; if (person == Person.Empty) MessageBox.Show("Default item selected!"); }
Sergey Berezovskiy
source share