当向组合框添加数据时,组合框所选索引更改为0
本文关键字:组合 索引 添加 数据 | 更新日期: 2023-09-27 18:06:33
嗨,我在load
表单中使用以下代码Combobox1.DataSource=GetItems();
默认选择第一项
我假设你的ComboBox有DropDownStyle属性设置为DropDownList。如果是,设置Datasource会自动将SelectedIndex设置为0(列表中的第一个元素)。你可以这样写:
Combobox1.DataSource=GetItems();
Combobox1.SelectedIndex = -1;
你不是在追加数据,你是在完全替换它。selecteindex会被重置。你可以记住它,然后像这样设置它
int oldIndex = Combobox1.SelectedIndex;
Combobox1.DataSource= GetItems();
Combobox1.SelectedIndex = oldIndex; //should check to see if the new list is long enough.