开发者

Combobox Selected Index is changing to 0 when data is appended to it

开发者 https://www.devze.com 2023-03-30 11:48 出处:网络
Hi开发者_JAVA技巧 I am using the following code in the form load Combobox1.DataSource=GetItems();

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.
0

精彩评论

暂无评论...
验证码 换一张
取 消