Home

List-Based Controls Data Binding

 

Data Display with 

Besides the DataGrid, the .NET Framework provides many other controls that are list-based. These include the combo boxes, list boxes, three views, list views, etc. These controls are ready for data display so much that they is little or not code involved but they must be appropriately configured. The list-based controls are usually meant to display data from another table. For this reason, they may use a foreign key that represents a child table. This means that, as done for the master/detail scenario, you should first have a parent, then a child table and the child table must have a foreign key that represents the table of the records that would be displayed on the list-based control.

 

Practical LearningPractical Learning: Binding Data With a Combo Box

  1. In Server Explorer, under the Tables node of CIC1, right-click Orders and click Design Table
  2. To add a new column, right-click OrderDate and click Insert Column
  3. Change the properties of the new column as follows:
    Column Name: EmployeeID
    Data Type: int
    Allow Nulls: cleared
    Default Value: 1
  4. Right-click anywhere in the table and click Relationships...
  5. In the Property Pages, click New
  6. In the Primary Key Table combo box, select Employees
  7. In the first combo box under it, select EmployeeID
  8. In the combo box under Orders, select EmployeeID
     
  9. Click Close and close the table
  10. When asked whether you want to save, click Yes twice
  11. To add a new form, on the main menu, click Project -> Add Windows Form...
  12. Set the Name to OrderProcessing and press Enter
  13. Change its Icon to the above App.ico and change its Text to
    Clarksville Ice Cream - Order Processing
  14. Set its StartPosition to CenterScreen
  15. Design the form as follows:
     
    Control Name Text Additional Properties
    Label   Order ID:  
    TextBox txtOrderID   ReadOnly: True
    Label   Processed By:  
    ComboBox cboEmployeeID   DropDownStyle: DropDownList
    Label   Order Date:  
    DateTimePicker dtpOrderDate   Format: Short
    Label   Order Time:  
    DateTimePicker dtpOrderTime   Format: Time:
    ShowUpDown: True
    Label   Flavor:  
    ComboBox cboFlavorID   DropDownStyle: DropDownList
    Label   Container:  
    ComboBox cboContainerID   DropDownStyle: DropDownList
    Label   Ingredient:  
    ComboBox cboIngredientID   DropDownStyle: DropDownList
    Label   Scoops:  
    TextBox txtScoops 1 TextAlign: Right
    Label   Total Order:  
    TextBox txtTotalOrder 0.00 TextAlign: Right
    Button btnClose Close  
    Button btnFirst | <  
    Button btnPrevious <  
    Button btnNext >  
    Button btnLast > |  
  16. In Server Explorer, under the Table node, click Containers
  17. Press and hold Ctrl
  18. Click Employees, Flavors, Ingredients, and Orders
  19. Release Ctrl
  20. Drag the selection and drop it on the form
     
  21. On the main menu, click Data -> Generate Dataset...
  22. Accept the Existing radio button
     
  23. Click OK
  24. On the form, click each control and, in the Properties window, bind it as follows
     
    Control DataBindings DataSource DisplayMember ValueMember
    Text Value SelectedValue
    txtOrderID DsIceCream1.
    Orders.OrderID
             
    cboEmployeeID     DsIceCream1.
    Orders.EmployeeID
    dsOrders1.Employees LastName EmployeeID
    dtpOrderDate   DsIceCream1.
    Orders.OrderDate
           
    dtpOrderTime   DsIceCream1.
    Orders.OrderTime
           
    cboFlavorID     DsIceCream1.
    Orders.FlavorID
    dsOrders1.Flavors Flavor FlavorID
    cboContainerID     DsIceCream1.
    Orders.ContainerID
    dsOrders1.Containers Container ContainerID
    cboIngredientID     DsIceCream1.
    Orders.IngredientID
    dsOrders1.Ingredients Ingredient IngredientID
    txtScoops DsIceCream1.
    Orders.Scoops
             
  25. Double-click an empty area of the form and implement the form's Load event as follows:
     
    Private Sub OrderProcessing_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.SqlDataAdapter5.Fill(Me.DsIceCream1)
            Me.SqlDataAdapter4.Fill(Me.DsIceCream1)
            Me.SqlDataAdapter3.Fill(Me.DsIceCream1)
            Me.SqlDataAdapter2.Fill(Me.DsIceCream1)
            Me.SqlDataAdapter1.Fill(Me.DsIceCream1)
    End Sub
  26. In the Class Name combo box, select btnClose. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Close()
    End Sub
  27. In the Class Name combo box, select btnFirst. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFirst.Click
            Me.BindingContext(Me.DsIceCream1, "Orders").Position = 0
        End Sub
  28. In the Class Name combo box, select btnPrevious. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
            Me.BindingContext(Me.DsIceCream1, "Orders").Position = _
            Me.BindingContext(Me.DsIceCream1, "Orders").Position - 1
        End Sub
  29. In the Class Name combo box, select btnNext. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
            Me.BindingContext(Me.DsIceCream1, "Orders").Position = _
            Me.BindingContext(Me.DsIceCream1, "Orders").Position + 1
        End Sub
  30. In the Class Name combo box, select btnLast. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLast.Click
            Me.BindingContext(Me.DsIceCream1, "Orders").Position = _
            Me.BindingContext(Me.DsIceCream1, "Orders").Count - 1
        End Sub
  31. Display the first form, Form1.vb [Design]
  32. On the Toolbox, click Button and click the form
  33. Change the new button's Text to Order Processing and change its Name to btnOrders
  34. Add another button to the form. Change its Text to Close and its Name to btnClose
  35. Reposition the buttons as follows:
     
    Ice Cream
  36. Double-click the Order Processing and implement its Click event as follows:
     
    Private Sub btnOrders_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOrders.Click
            Dim frmOrders As OrderProcessing = New OrderProcessing
            frmOrders.Show()
    End Sub
  37. In the Class Name combo box, select btnClose. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
            End
    End Sub
  38. Execute the application and click the different buttons
     
  39. Close the forms
 

Previous Copyright © 2004-2010 FunctionX, Inc.