,可以通过以下步骤实现:
下面是一个示例代码:
Imports System.Data.SqlClient
Public Class Form1
Private connectionString As String = "YourConnectionString"
Private dataTable As New DataTable()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 初始化第一个组合框的数据源
comboBox1.DataSource = GetComboBox1Data()
comboBox1.DisplayMember = "CategoryName"
comboBox1.ValueMember = "CategoryID"
' 初始化第二个组合框的数据源
comboBox2.DataSource = GetComboBox2Data()
comboBox2.DisplayMember = "ProductName"
comboBox2.ValueMember = "ProductID"
End Sub
Private Sub comboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboBox1.SelectedIndexChanged
' 根据第一个组合框的选择值筛选数据
Dim categoryId As Integer = CInt(comboBox1.SelectedValue)
Dim filteredData = dataTable.AsEnumerable().Where(Function(row) row.Field(Of Integer)("CategoryID") = categoryId).CopyToDataTable()
' 更新数据网格视图的显示
dataGridView1.DataSource = filteredData
End Sub
Private Sub comboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles comboBox2.SelectedIndexChanged
' 根据第二个组合框的选择值筛选数据
Dim productId As Integer = CInt(comboBox2.SelectedValue)
Dim filteredData = dataTable.AsEnumerable().Where(Function(row) row.Field(Of Integer)("ProductID") = productId).CopyToDataTable()
' 更新数据网格视图的显示
dataGridView1.DataSource = filteredData
End Sub
Private Function GetComboBox1Data() As DataTable
' 获取第一个组合框的数据源
Dim query As String = "SELECT DISTINCT CategoryID, CategoryName FROM Products"
Dim adapter As New SqlDataAdapter(query, connectionString)
adapter.Fill(dataTable)
Return dataTable
End Function
Private Function GetComboBox2Data() As DataTable
' 获取第二个组合框的数据源
Dim query As String = "SELECT DISTINCT ProductID, ProductName FROM Products"
Dim adapter As New SqlDataAdapter(query, connectionString)
adapter.Fill(dataTable)
Return dataTable
End Function
End Class
在上述示例代码中,需要替换YourConnectionString
为实际的数据库连接字符串。同时,需要根据实际情况修改SQL查询语句和数据表的结构。
这个示例中使用了一个名为"Products"的数据表,其中包含了"CategoryID"和"ProductID"两个列,分别表示产品的类别和ID。根据选择的类别和产品,筛选出对应的数据,并将结果显示在数据网格视图中。
对于这个示例,可以使用腾讯云的数据库产品TencentDB来存储和管理数据。具体的产品介绍和链接地址可以参考腾讯云的官方文档:TencentDB产品介绍。
领取专属 10元无门槛券
手把手带您无忧上云