2011-10-22 66 views
0

我仍然遇到组合框的问题。我有正确的查询。我有MS Access中的四个表。我需要在Visual Basic中创建一个包含组合框和数据网格的窗体。从组合框中选择将在数据网格上显示关于该人的所有相关信息。Visual Basic 2010组合框到datagrid

例如,如果我选择李四(从下拉列表框),DataGrid中应显示:

CUSTOMER_NAME(李四)order_date的(01/01/01)项目(小工具)的价格(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;` 

如何添加李四到组合框,当在此查询链接到它,选择出的,它显示的结果在DataGrid?

谢谢。任何帮助表示赞赏。

回答

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