我仍然遇到组合框的问题。我有正确的查询。我在MS Access中有四个表。我需要在Visual Basic中创建一个具有组合框和数据网格的表单。从组合框中选择将在数据网格上显示有关该人员的所有相关信息。
例如,如果我选择John Doe(来自组合框),数据网格应显示:
customer_name(John Doe)order_date(01/01/01)item(Widget)price(9.99)
我的查询是:
`SELECT customers.customer_name, customers.customer_id, orders.order_date,
orders.order_id, items.item_description, items.item_id, items.item_price
FROM (customers, orders, items)
LEFT JOIN order_items ON orders.order_id = order_items.order_id
AND items.item_id = order_items.item_id
HERE customers.customer_name = 'John Doe'
`AND customers.customer_id = orders.order_id
ORDER BY orders.order_id, items.item_id;`
如何将John Doe添加到组合框并将此查询链接到它,在被选中时,它会在数据网格中显示结果?
谢谢。任何帮助表示赞赏。
答案 0 :(得分:0)
首先尝试将客户加载到组合框中,创建在用户选择值时触发的事件,然后获取该客户的数据并将其绑定到datagridview
Private Sub LoadCustomers()
Dim cmd As New SqlCommand
cmd.CommandText = "SELECT customers.customer_name, customers.customer_id FROM customers "
cmd.Connection = yourConnectionObject
Dim myCombo As ComboBox
Dim ds As DataSet
Dim da As SqlDataAdapter
Try
da.SelectCommand = cmd
da.Fill(ds, "Customers")
' this will load customers to combobox
With myCombo
.DataSource = ds.Tables(0).DefaultView
.DisplayMember = "customer_name"
.ValueMember = "customer_id"
End With
Catch ex As Exception
Finally
End Try
End Sub
' create one event that fires when the user changes the selected value on the combobox
' this is only a example, create your own method properly
Private Sub combobox_selectedvaluechanged() Handles combobox.SelectedValueChanged()
Dim CustomerId As Integer
Try
' here you get the selected customer id
CustomerId = ComboBox.SelectedValue
' Now you can retrieve data for this customer with your sql query
' and for better results use the customer id to run the query
Dim ds As DataSet = GetDataForCustomer(CustomerId) ' as dataset
' bind the returned dataset to the datagrid
yourdatagrid.Datasource = ds
Catch ex As Exception
End Try
End Sub