-2

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

coderblogger
  • 84
  • 11
rul3z
  • 173
  • 1
  • 16

2 Answers2

0

Without the exception, hard to tell.

By the way, focusing on this line, I see 2 problems could arise :

Int32.Parse(cmbDistaccamento.SelectedValue.ToString())
  1. SelectedValue can be null -> NullException
  2. SelectedValue.ToString() can fail when parsed as an Int
Panda
  • 448
  • 2
  • 8
  • Exception message : The format string is not correct . Ok the exception ....... but the problem is to read the value after event SelectedIndexChanged.. I need the " key" value assigned to the combobox which selects with the SelectIndexChanged. But SelectIndexChanged it's run twince with a from load ! – rul3z Mar 09 '16 at 16:14
  • I haven't tested, but you should set your dataSource **after** setting the DisplayMember and ValueMember. – Panda Mar 10 '16 at 07:06
0
private void frmInserisciCollaboratore_Load(object sender, EventArgs e)
        {
            //cmbDistaccamento.BeginUpdate();
            QueryAssist queryAssist = new QueryAssist();
            DataTable dataTable = new DataTable();
            dataTable = queryAssist.runQuery("SELECT DISTACCAMENTO.ID_Distaccamento, DISTACCAMENTO.Indirizzo FROM DISTACCAMENTO");

            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.DisplayMember = "value";
            cmbDistaccamento.ValueMember = "key";
            cmbDistaccamento.DataSource = new BindingSource(comboSource, null);
            //cmbDistaccamento.EndUpdate();
        }

and

private int i = 0;
        private void cmbDistaccamento_SelectedIndexChanged(object sender, EventArgs e)
        {

            try
            {
                i += 1;
                MessageBox.Show("print iteration i : " +i.ToString());

                _cmbDistaccamentoResult = Int32.Parse(cmbDistaccamento.SelectedValue.ToString());
                MessageBox.Show(_cmbDistaccamentoResult.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);
            }
        }

now result is :' print iteration 1' after formload and next time i have the possibility to choose of combobox.. this is the problem. The first raise SelectedItemChanged on formload. ... :(

rul3z
  • 173
  • 1
  • 16