Home

Data Binding Windows Controls

 

Data Navigation With Windows Controls

 

Introduction

Although the records of a database are stored in tables, data sheets sometimes provide unfriendly environment for data entry. Of course, to avoid using database tables that can appear boring, you can use the DataGrid control. One of the characteristics of data grids is that they display all of their record set, or at least as much as their width and height can allow. If some fields require much room, a data grid as good looking as it can be, would not be suitable. Imagine that one of the columns contains fields of text of various paragraphs. In this case, the records should be displayed one at a time.

If you want to display one record at a time on a form, you can use Windows controls and provide a means for the user to navigate back and forth in the records.

Practical LearningPractical Learning: Display Data in Windows Controls

  1. To add a new form, on the main menu, click Project -> Add Windows Form...
  2. Replace the Name of the form with Flavors and press Enter
  3. Change its Icon to the above cic.ico and change its Text to Clarksville Ice Cream - Flavors
  4. Set its ShowInTaskbar property to False and its StartPosition to CenterScreen
  5. From the Server Explorer, under the Tables node of CIC1, drag the Flavors table and drop it on the form
  6. On the main menu, click Data -> Generate Dataset...
  7. In the Generate Dataset dialog box, accept the Existing radio button  and click OK
  8. Click the Toolbox tab, click Windows Forms, and select the controls to design the form as follows:
     
    Control Name DataBindings -> Text
    dsIceCream - Flavors.
    Text Other Properties
    Form        
    Label     Flavor ID:  
    TextBox txtFlavorID FlavorID   ReadOnly: True
    TextAlign: Right
    Label     Flavor:  
    TextBox txtFlavor Flavor  
    Label     Composition:  
    TextBox txtComposition Composition   Multiline: True
    ScrollBars: Vertical
    Button btnClose   Close   
    Button btnFirst   | <  
    Button btnPrevious   <  
    Button btnNext   >  
    Button btnLast   > |  
  9. Double-click an empty area on the form to access its Load event and implement it as follows:
     
    Private Sub Flavors_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.SqlDataAdapter1.Fill(Me.DsIceCream1)
    End Sub
  10. 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
  11. 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, "Flavors").Position = 0
    End Sub
  12. 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, "Flavors").Position = _
            Me.BindingContext(Me.DsIceCream1, "Flavors").Position - 1
    End Sub
  13. 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, "Flavors").Position = _
            Me.BindingContext(Me.DsIceCream1, "Flavors").Position + 1
    End Sub
  14. 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, "Flavors").Position = _
            Me.BindingContext(Me.DsIceCream1, "Flavors").Count - 1
    End Sub
  15. Display the first form, Form1.vb [Design]
  16. On the Toolbox, click Button and click under the existing Employees button of the form
  17. Change the new button's Text to Flavors and change its Name to btnFlavors
  18. Double-click the new Flavors button and implement its Click event as follows:
     
    Private Sub btnFlavors_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFlavors.Click
            Dim frmFlavors As Flavors = New Flavors
            frmFlavors.ShowDialog()
    End Sub
  19. Execute the application
  20. On the form, click the Flavors button
     
    Flavors
  21. Close the forms and return to your programming environment
 

Previous Copyright © 2004-2010 FunctionX, Inc. Next