I using a System.Windows.Forms.ComboBox and I'm getting some weird unexpected behavior. In c#, I am dynically adding a few comboBoxes to my form and binding them to a list. The only fields I'm setting are DataSource,ValueMember, and DisplayMember. For some reason, after I bind to the list, the first item is selected. I cannot figure out what's going on.
My code looks like this:
Control c = new System.Windows.Forms.ComboBox();
Looping through all my controls,
if (c?.GetType() == typeof (ComboBox))
{
BindComboBox((ComboBox) c);
}
private void BindComboBox(ComboBox sender)
{
DataTable table = DataGateway.GetTables(1);
sender.DataSource = table;
sender.ValueMember = "ID";
sender.DisplayMember = "Name";
//sender.SelectedIndex = -1; I tried with this and without this
}
I also tried a second method but the same thing is happening -
private void BindComboBox(ComboBox sender)
{
List<string> hiStrings = new List<string>() {"hi", "hello", "whats up"};
sender.DataSource = hiStrings;
}