I have a problem with a event SelectIndexChanged after populating with a Database. The combobox is populating in FORMLOAD event
QueryAssist queryAssist = new QueryAssist();
DataTable dataTable = new DataTable();
dataTable = queryAssist.runQuery(_query);
Dictionary<int, string> comboSource = new Dictionary<int, string>();
comboSource.Add(-1, "Select");
foreach (DataRow dr in dataTable.Rows)
{
comboSource.Add((int)dr.ItemArray[0], (string)dr.ItemArray[1]);
}
cmbDistaccamento.DataSource = new BindingSource(comboSource, null);
cmbDistaccamento.DisplayMember = "value";
cmbDistaccamento.ValueMember = "key";
the event SelectedIndexChanged
int i = 0;
private void cmbDistaccamento_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
i += 1;
MessageBox.Show(i.ToString());
_cmbDistaccamentoResult = Int32.Parse(cmbDistaccamento.SelectedValue.ToString());
//Convert.ToInt32((cmbDistaccamento.SelectedValue.ToString()));
}
catch (Exception ex)
{
MessageBox.Show("Impossibile convertire il valore(value) combobox da string a int \r\n" + ex.Message);
}
}
MessageBox Show 2 ...
An exception is raised. I think because 'cmbDistaccamento' take different value before to taking a string.
I need the " key" value assigned to the combobox which selects with the SelectIndexChanged.
I try to use beginUpdate() and endUpdate() methods but not work..
How to resolve this problem ?
Sorry for bad english