how to get selected dataRow from combobox

18,166

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;
Share:
18,166
MAW74656
Author by

MAW74656

Updated on June 04, 2022

Comments

  • MAW74656
    MAW74656 almost 2 years

    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?