1
votes

I have two Comboboxes and a DataGridView. the first ComboBox looks at column of the DataGridView for its values. I need the second ComboBox to look at the value of the first and then get the values from the table filtered by the Value of the first ComboBox.

I get the information for the first ComboBox like this: (This runs on the "Form Load" Event)

    With FItemTypeComboBox
        .DataSource = DbStarFliteSystemsDataset.tblItems
        .ValueMember = "fItemType"
    End With

For the second ComboBox's Datasource, I want to run a query that functions something like:

    SELECT DISTINCT fItemName FROM tblItems
    WHERE "fItemType" = FItemTypeComboBox.Text

But I don't know where or how to implement this.

I have tried the following, but no matter what variation I run, it always fails:

Private Sub FItemNameComboBox_Enter(sender As Object, e As EventArgs) Handles FItemNameComboBox.Enter

    Dim table As DataTable = New DataTable("tblItems")

    table.Columns.Add("fItemType")
    table.Columns.Add("fItemName")

    Dim Result() As DataRow = table.Select("fItemType" = FItemTypeComboBox.Text)

    With FItemNameComboBox
        .DataSource = Result
        .ValueMember = Result.ToString
    End With

End Sub

I am using Visual Studio 2013 and (obviously) Visual Basic.

1
What is the data type of the returned linq query? - Bjørn-Roger Kringsjå
They are all standard Strings. Basically I am looking to filter a large table into categories, then select an item from that category and add it to a different table. - ZLHysong
I would like to know the exact data type. Is it a String(), IEnumerable(Of String), IList(Of String)? :) - Bjørn-Roger Kringsjå
I don't know. The table that is used in DbStarFliteSystemsDataset.tblItems lists fItemType as nvarchar(MAX), Does that help? - ZLHysong
Doesn't IntelliSense show you the type? Example: Dim result As Object = (From row As DataRow In table Where row.Field(Of Integer)("id") = 1 Select row). If I hover the mouse over the first ( in Dim result As Object = (... I see that the returned result is EnumerableRowCollection(Of DataRow). - Bjørn-Roger Kringsjå

1 Answers

1
votes

You can do it using a DataView. A DataView act like a wrapper for a DataTable, supporting editing, filtering and sorting.

Private Sub FItemTypeComboBox_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles FItemTypeComboBox.SelectedIndexChanged

    Dim selected As Object = Me.FItemTypeComboBox.SelectedItem

    If (TypeOf selected Is DataRowView) Then

        Dim row As DataRow = DirectCast(selected, DataRowView).Row
        Dim fItemType As String = CStr(row.Item("fItemType"))
        Dim view As New DataView(DbStarFliteSystemsDataset.tblItems)

        view.RowFilter = String.Format("[fItemType]='{0}'", fItemType)

        Me.FItemNameComboBox.DataSource = view.ToTable(True, "fItemName")
        Me.FItemNameComboBox.DisplayMember = "fItemName"

    End If

End Sub