Background
Lately, I have observed two undesirable behaviors of the Winform ComboBox
control:
- Setting the
DataSource
property to a new object sets theSelectedIndex
value to 0 - Setting the
DataSource
property to a previously used object "remembers" the previouslySelectedIndex
value
Here is some sample code to illustrate this:
private void Form_Load(object sender, EventArgs e)
{
string[] list1 = new string[] { "A", "B", "C" };
string[] list2 = new string[] { "D", "E", "F" };
Debug.Print("Setting Data Source: list1");
comboBox.DataSource = list1;
Debug.Print("Setting SelectedIndex = 1");
comboBox.SelectedIndex = 1;
Debug.Print("Setting Data Source: list2");
comboBox.DataSource = list2;
Debug.Print("Setting SelectedIndex = 2");
comboBox.SelectedIndex = 2;
Debug.Print("Setting Data Source: list1");
comboBox.DataSource = list1;
this.Close();
}
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
Debug.Print("Selected Index Changed, SelectedIndex: {0}", comboBox.SelectedIndex);
}
private void comboBox_DataSourceChanged(object sender, EventArgs e)
{
Debug.Print("Data Source Changed, SelectedIndex: {0}", comboBox.SelectedIndex);
}
This produces the following output:
Setting Data Source: list1 Data Source Changed, SelectedIndex: -1 Selected Index Changed, SelectedIndex: 0 Setting SelectedIndex = 1 Selected Index Changed, SelectedIndex: 1 Setting Data Source: list2 Data Source Changed, SelectedIndex: 1 Selected Index Changed, SelectedIndex: 0 Setting SelectedIndex = 2 Selected Index Changed, SelectedIndex: 2 Setting Data Source: list1 Data Source Changed, SelectedIndex: 2 Selected Index Changed, SelectedIndex: 1
It is this last debug statement that is particularly interesting.
Questions
How is this possible and is there anyway to prevent this behavior?
Is the ComboBox's "memory" indefinite, or is it supported by objects which are susceptible to garbage collection? The former case means adjusting DataSource results in memory consumption, the latter case indicates the ComboBox's behavior is not predictable when setting the DataSource.
comboBox.DataSource = new BindingSource(list1, null);
– LarsTechDataSource
to a newBindingSource
prevents the "Memory" from occuring (e.g.DataSource = new BindingSource { DataSource = list1 };
. If you want to write that up as an answer, I'll probably accept it as a decent workaround. Still interested in knowing the underpinnings of the default behavior though... – nicholasDataSource
to the newBindingSource
inbetween the traditional assignments, it would fix it. Now I understand that you mean to always useBindingSource
as the source. (Also, this approach lets you set a default value by setting thePosition
property: except -1 is not supported). – nicholas