Search code examples
c#.netwinformscombobox

how to get selected dataRow from combobox


I have a combobox which is bound to a dataset. I'm trying to get the DataRow the text of the combobox represents, but I can't find it. I've tried the following:

 private void cboItems_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable flexoItems = (cboItems.DataSource as DataTable);

        DataRow row = flexoItems.Rows.Find(cboItems.Text);

        //DataView view = new DataView(flexoItems);
        //DataRow row = flexoItems.Rows[view.Find(cboItems.Text)];

        lblItemDesc.Text = row["Description"].ToString();
        lblTotalQty.Text = row["QtyOnHand"].ToString();
    }

I feel like I'm just missing this. How can I get the other values from the row of a combobox selection?


Solution

  • I am assuming you used data binding to populate the combo box. In that case use the SelectedItem property of combo box. It will probably contain a DataRowView, so you can use code like this.

    DataRowView vrow = (DataRowView)cboItems.SelectedItem;
    DataRow row = vrow.Row;