3

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?

7
  • Well i think you just get the ID from combo box and pass it to database and get all data related to that ID. what you say?
    – Emaad Ali
    Sep 14, 2011 at 16:59
  • The ID which is unique and when you pass that ID in database then you get only its data.
    – Emaad Ali
    Sep 14, 2011 at 17:03
  • In flexoItems only these two column exists "Description" and "QtyOnHand"?
    – Emaad Ali
    Sep 14, 2011 at 17:04
  • flexoItems contains "ItemNumber" (which is displayed in the combobox, and "Description" and "QtyOnHand". I still don't understand what your suggesting with the ID.
    – MAW74656
    Sep 14, 2011 at 17:06
  • 1
    Ok, so you want to get the whole data of selected Item correct?
    – Emaad Ali
    Sep 14, 2011 at 17:08

3 Answers 3

16

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;
0

How to get selected row from datacombo's datasource on VB .NET VB2010, VB2012.

This should work on VB 2010 and up if you have a form with a datacombo bound to a DataBindingSource in the form.

Dim dr As DataRowView = LOCAL_FORMBindingSource.Current
Dim r As DataSet.MY_TABLERow = CType(dr.Row, DataSet.MY_TABLERow)

MY_FIELDTextBox.Text = r.MY_FIELD

I have added this info here since I was not able to find it anywhere else or found here any option to post it on the appropriate VB forum.

0
Private Function GetRowDataText(col As Integer) As String
    Try
        Dim dRow As DataRowView=Type(comboBoxName.SelectedItem,DataRowView)
        Return dRow.Row.ItemArray(col)            
    Catch ex As Exception
        Return String.Empty
    End Try
End Function
1
  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. Jun 9, 2022 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.