Hi开发者_JAVA技巧 I am using the following code in the form load
Combobox1.DataSource=GetItems();
Then by default first item is selected.
I suppose your ComboBox has the DropDownStyle property set to DropDownList. When it is, setting the Datasource automatically sets the SelectedIndex to 0 (first element in the list). You could write:
Combobox1.DataSource=GetItems();
Combobox1.SelectedIndex = -1;
You're not appending data, you're replacing it entirely. So the SelectedIndex will be reset. You could remember it and then set it back like so
int oldIndex = Combobox1.SelectedIndex;
Combobox1.DataSource= GetItems();
Combobox1.SelectedIndex = oldIndex; //should check to see if the new list is long enough.
精彩评论