How to capture a selected item from a combobox filled from a BD

1

I need help with a bit of code. It turns out that I have a combobox that I fill using a database:

private void frmMain_Load(object sender, EventArgs e)
    {
        //TEXT BOX DISABLED
        txtDv.Enabled = false;
        txtNombre.Enabled = false;
        txtApellido.Enabled = false;
        txtCorreo.Enabled = false;
        txtTelefono.Enabled = false;

        //LLENAR COMBOBOX DESDE BD
        using (SqlConnection conn = new SqlConnection("Data Source = DESKTOP-0PSJQKP; initial Catalog = BDControlEPP; User = sa; Password = a123456"))
        {
            try
            {
                string query = "select idItem, nombreItem from tblItems";
                SqlDataAdapter da = new SqlDataAdapter(query, conn);
                conn.Open();
                DataSet ds = new DataSet();
                da.Fill(ds, "Items");
                cbItems.DisplayMember = "nombreItem";
                cbItems.ValueMember = "idItem";
                cbItems.DataSource = ds.Tables["Items"];

            }
            catch (Exception)
            {

                throw;
            }
        }

    }

And I need to capture the selected item in the combobox with an ADD button that I have beside it, plus a text box next to it that would indicate the amount I want of that item and insert it into a Listview in the same form.

I remain attentive to any response, greetings!

    
asked by Esteban Ugalde Alarcon 12.03.2018 в 22:03
source

1 answer

1

If you do not misinterpret yourself to get the value of a ComboBox you have 3 options ..

Get the text written in the combo

string texto = comboBox1.SelectedText

Get the value member of the selected option in the combobox

string miembro = comboBox1.SelectedValue.ToString();

Finally, get the position index of the selected element

int posicion = comboBox1.SelectedIndex;

However, from what I see in the comments, if what you need is to get the rest of the values based on the value member of the comboBox, what you should do is subscribe the ComboBox to the SelectedIndexChanged and each time that event occurs perform the query you need based on the value obtained in the comboBox

    
answered by 13.03.2018 в 05:12