I have a combobox which is bound to a list of DictionaryEntries. And I would like that when the form is shown no item is selected (SelectedItem = null).
Test program
The problem is that when the form is shown DropDownList automatically selects first element in the list (SelectedIndex, SelectedItem,.. are updated but Text is not).
If I rebind after the form is shown DropDownList works correctly.
Can I bind a list in a way that Form.Show will not select the first element in the list (SelectedItem = null)?
Test program
public
partial
class
Form1 : Form
{
public
Form1()
{
InitializeComponent();
Bind(GetList());
Print(
"Before show"
);
}
public
void
Bind(
List<DictionaryEntry> p_List)
{
radDropDownList1.ValueMember =
"Key"
;
radDropDownList1.DisplayMember =
"Value"
;
radDropDownList1.DataSource = p_List;
radDropDownList1.SelectedIndex = -1;
radDropDownList1.SelectedItem =
null
;
radDropDownList1.SelectedValue =
null
;
}
private
List<DictionaryEntry> GetList()
{
return
new
List<DictionaryEntry>
{
new
DictionaryEntry(0,
"Entry 0"
),
new
DictionaryEntry(1,
"Entry 1"
)
};
}
private
void
button1_Click(
object
sender, EventArgs e)
{
Bind(GetList());
Print(
"rebind"
);
}
private
void
Print(
string
p_Message)
{
richTextBox1.AppendText(String.Format(
"{2}\nSelectedIndex = {0}\nSelectedValue = {1}\n\n"
,
radDropDownList1.SelectedIndex, radDropDownList1.SelectedValue, p_Message));
}
private
void
Form1_Shown(
object
sender, EventArgs e)
{
Print(
"OnShow"
);
}
private
void
button2_Click(
object
sender, EventArgs e)
{
Print(
"Print state"
);
}
}
The problem is that when the form is shown DropDownList automatically selects first element in the list (SelectedIndex, SelectedItem,.. are updated but Text is not).
If I rebind after the form is shown DropDownList works correctly.
Can I bind a list in a way that Form.Show will not select the first element in the list (SelectedItem = null)?