2011-10-22 70 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