Get the value of the ID in a ComboBox

2

Good day of your support with this topic, I am creating a Windows Forms in which I have a ComboBox I load from a table in SQL this table only has 2 records so far and I want to get the ID of the item selected to then store it in another table, I hope you can help me, since I do not remember how to do it.

public void CargaComboClientes()
    {
        try
        {
            dtClientes = objConsultas.MuestraClientes();
            if (dtClientes.Rows.Count!=0)
            {
                cmbClientes.DataSource = dtClientes;
                cmbClientes.ValueMember = "Cte_ID";
                cmbClientes.DisplayMember = "Cte_RazonSocial";
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }

private void cmbClientes_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        int cteId = 0;
        if (cmbClientes.SelectedValue!=null)
        {
            cteId = Convert.ToInt32(cmbClientes.SelectedValue);
        }
    }
    catch (Exception ex)
    {
        ex.ToString();
    }
}

And doing it this way and wanting to get the value and save it in a variable shows me this exception.

No se puede convertir un objeto de tipo 'System.Data.DataRowView' al tipo 'System.IConvertible'.
    
asked by user66626 17.11.2017 в 18:10
source

3 answers

1

It's a little more laborious, I know. Call me scenic but I do not like to drag the datasets more than necessary, and then the lists make life much easier.

    private struct Data
    {
        int Cte_ID { get; set; }
        string Cte_RazonSocial { get; set; }
        string Otro_Campo { get;set; }
    }

    public void CargaComboClientes()
    {
        List<Data> datos = new List<Data>();

        foreach (DataRow row in objConsultas.MuestraClientes().Rows)
        {
            datos.add
                (
                    new Data { 
                               Cte_ID = Int32.Parse(row["Cte_ID "])
                             , Cte_RazonSocial = row["Cte_RazonSocial"] 
                             , Otro_Campo = row["Otro_Campo"] 
                             }
                );
        }

        cmbClientes.DataSource = datos;
        cmbClientes.DisplayMember = "Cte_RazonSocial";
    }

    private void cmbClientes_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cmbClientes.SelectedItem!= null)
        {
            Data seleccionado = (Data)cmbClientes.SelectedItem;
            MessageBox.Show("Usted ha seleccionado el ID " + seleccionado.Cte_ID.ToString() + ", con " + seleccionado.Otro_Campo);
        }
    }

    //creo que está correcto
    
answered by 11.09.2018 в 14:24
0

In this part:

    if (cmbClientes.SelectedValue!=null)
    {
        cteId = Convert.ToInt32(cmbClientes.SelectedValue);
    }

change it to:

    if (cmbClientes.SelectedValue!=null)
    {
        var item = cmbClientes.SelectedValue as DataRowView;
        if (item != null)
        {            
            cteId = Int32.Parse(item.Row["Cte_ID"]);
        }
    }

Or if you use c # version 7 (Visual Studio 2017):

    if (cmbClientes.SelectedValue!=null)
    {
        if (cmbClientes.SelectedValue is DataRowView item)
        {            
            cteId = Int32.Parse(item.Row["Cte_ID"]);
        }
    }
    
answered by 17.11.2017 в 18:14
0

If you change the order of the DataSource, valuemember and displaymember assignments, it should work correctly:

public void CargaComboClientes()
    {
        try
        {
            dtClientes = objConsultas.MuestraClientes();
            if (dtClientes.Rows.Count!=0)
            {
                cmbClientes.ValueMember = "Cte_ID";
                cmbClientes.DisplayMember = "Cte_RazonSocial";
                cmbClientes.DataSource = dtClientes;
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }

The problem you have is that when you execute this line in your code cmbClientes.ValueMember="Cte_ID"; the SelectedIndexChanged event triggers but you have not yet assigned the value to valuemember.

    
answered by 11.09.2018 в 15:42