我有两个Comboboxes和一个DataGridView。第一个ComboBox查看DataGridView的列以获取其值。我需要第二个ComboBox来查看第一个的值,然后从表中获取第一个ComboBox的值过滤的值。
我得到第一个ComboBox的信息如下:(这是在“表单加载”事件上运行的)
With FItemTypeComboBox
.DataSource = DbStarFliteSystemsDataset.tblItems
.ValueMember = "fItemType"
End With
对于第二个ComboBox的数据源,我想运行一个类似于以下内容的查询:
SELECT DISTINCT fItemName FROM tblItems
WHERE "fItemType" = FItemTypeComboBox.Text
但我不知道在哪里或如何实现这一点。
我尝试过以下操作,但无论我运行什么变体,都会失败:
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
我正在使用Visual Studio 2013和(显然)Visual Basic。
答案 0 :(得分:1)
您可以使用DataView来完成此操作。 DataView就像是DataTable的包装器,支持编辑,过滤和排序。
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