Danh mục

Bind Data to ComboBox and DataGrid Controls

Số trang: 5      Loại file: pdf      Dung lượng: 20.87 KB      Lượt xem: 12      Lượt tải: 0    
Hoai.2512

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

1,8 Bind dữ liệu vào ComboBox và DataGrid Controls Đôi khi bạn có thể muốn sử dụng điều khiển ComboBox thay vì một điều khiển ListBox để hiển thị một danh sách các lựa chọn. Bạn cũng có thể muốn hiển thị thông tin trong một phong cách lưới dựa trên các mục chọn trong hộp kết hợp. d kỹ thuật mã hóa mà bạn đã sử dụng cho điều khiển ListBox
Nội dung trích xuất từ tài liệu:
Bind Data to ComboBox and DataGrid Controls1.8 Bind Data to ComboBox and DataGrid ControlsSometimes you might want to use a ComboBox control instead of a ListBox control todisplay a list of choices. You also might want to display information in a grid style basedon the item chosen in that combo box. This How-To describes how to bind data to bothComboBox and DataGrid controls.Instead of using a ListBox control to display customers, you would like to use aComboBox control to display them. After you choose a customer, you would like todisplay information about the orders that belong to that customer. How do you bind datato the ComboBox and then bind the DataGrid control as well?TechniqueTo bind both the ComboBox and the DataGrid controls, you will use the same propertiesand coding techniques that you have used for the ListBox control. You will first createDataAdapter and DataSet controls to which to set the DataSource properties. TheDataAdapter for the DataGrid will include a parameter that will use the value selected inthe ComboBox control.You will set the DataSource properties of the controls to the DataSet created. That is allyou really have to set for the DataGrid control. For the ComboBox control, you will alsoset the ValueMember and DisplayMember properties.You will then add code to fill the DataSet control to which the DataSource property is setfor the ComboBox control. Finally, you will create a subroutine to store the selectedvalue in the ComboBox in the DataAdapter that was created for filling the DataSet onwhich the DataGrid control is based.After the user has selected a value in the ComboBox that contains the customers, theDataGrid control displays the orders for that customer, as can be seen in Figure 1.14. Figure 1.14. Users can display the header information for customer orders using these two controls.StepsYou will be starting with a fresh form. 1. Add a new Windows Form called frmHowTo1_8. 2. Add the OleDbDataAdapter and DataSet controls with the properties set forth in Table 1.7. Table 1.7. OleDataAdapter and DataSet Controls Object Property Setting OleDataAdapter Name odaCustomerList SelectCommand OleDbSelectCommand1 CommandText SELECT CustomerID, CompanyName FROM Customers DataSet Name dsCustomerList DataSetName dsCustomerList OleDataAdapter Name odaOrdersForCustomer SelectCommand OleDbSelectCommand2 CommandText SELECT OrderID, OrderDate, RequiredDate, ShippedDate FROM Orders WHERE (CustomerID = ?) ORDER BY OrderDateDataSet Name dsOrdersForCustomer DataSetName dsOrdersForCustomer3. Tip You will want to create the OleDbDataAdapter controls using the Data Adapter Configuration Wizard, and then set the name after the DataAdapter has been created. You will want to create the DataSet controls by right-clicking on the appropriate OleDbDataAdapter and choosing Generate Dataset. If you have questions about creating these controls, reread How-To 1.1. Also, remember that when generating the DataSet, VS always changes the name by adding a 1 on the end and capitalizing the first letter. You might want to change it to what you really want in the properties directly.4. Add the ComboBox and DataGrid controls, as well as the Label for the ComboBox, setting the properties as shown in Table 1.8. Table 1.8. Label, ComboBox, and DataGrid Controls Property Settings Object Property Setting Label Name Label1 Text Customers to List Orders For: ComboBox Name cboCustomers DataSource dsCustomerList.Customers DisplayMember CompanyName ValueMember CustomerID DataGrid Name dgOrders DataSource dgOrdersForCustomer.Orders5. Add code shown in Listing 1.23 to the Load event of the form. This code fills the dsCustomerList DataSet control based off the select statement used in the odaCustomerList OleDbDataAdapter control. After this occurs, the RefreshOrders subroutine is called, displayed in Listing 1.24. Listing 1.23 frmHowTo1_8.vb: Filling the Dataset Used by the ComboBox Control Private Sub frmHowTo1_8_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles MyBase.Load Me.odaCustomerList.Fill(Me.dsCustomerList) RefreshOrders() End Sub Listing 1.24 frmHowTo1_8.vb: Filling the Dataset Used by the DataGrid Control, Based on the Item Selected in the ComboBox Control Private Sub RefreshOrders() - Clear Orders for customer dataset Me.dsOrdersForCustomer.Clear() - Check to see if an item was selected If Me.cboCustomers.SelectedIndex -1 Then - Store the selected customer ID into the parameter the SQL data adapter Me.odaOrdersForCustomer.SelectCommand.Parameters(0).Value = _ Me.cboCustomers.SelectedItem(0) - Fill the dataset Me.odaOrdersForCustomer.Fill(Me.dsOrdersForCustomer) End If End Sub6. Add the code in Listing 1.25 to the SelectedIndexChanged event of the cboCustomers ComboBox controls. Listing 1.25 frmHowTo1_8.vb: Refreshing the DataGrid Control Based on ...

Tài liệu được xem nhiều: