MAW74656
MAW74656

Reputation: 3539

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?

Upvotes: 3

Views: 20089

Answers (3)

Sergio V
Sergio V

Reputation: 1

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

Upvotes: 0

Edgardo Pichardo
Edgardo Pichardo

Reputation: 1

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.

Upvotes: 0

Kleinux
Kleinux

Reputation: 1541

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;

Upvotes: 16

Related Questions