Home

DataGrid Binding

 

Overview of Data Navigation

 

Introduction

From the user's point of view, data navigation consists of moving from one piece of information of a database to another. This could be done from one control to another or from one record to another. To support the various scenarios of data navigation, there are three main categories of data display the user will face. Put it another way, there are three types of scenarios you as the database developer will present to the user.

Scenarios of Data Navigation

There are three main ways you display data to the user:

Datasheet Display: A datasheet displays its information in series of columns and rows, the intersections of which are called cells. A datasheet is meant to display as many records as possible all at once in the same view:

To navigate among cells of a datasheet, the user can click a value and click another as desired. Alternatively, the user can press Tab continuously to move from one cell to another. Some datasheet controls also allow pressing Enter to move from one cell to another. In some cases, some cells can be made to display controls such as check or combo boxes.

Windows Controls Display: While a datasheet displays its information in cells, you may prefer to use more elaborate controls to display data. This means that you can use edit boxes, list-based controls, button-based controls (such as radio buttons or check boxes), etc. Here is an example:

To navigate from one control to another, the user can click continuously. In most cases, the user can also press Tab to move among records.

An alternative to this scenario is to add a datasheet portion to the form, combined with other controls, in what is referred to as master/detail.

Record Navigation: A datasheet is meant to display all of its records or as many records as possible. The above display of various controls is used display one record at a time. When the user has finished using the record, such as during data entry, you must provide the user with a way to restart. An alternative is to allow the user to move from one record to another. Of course, this is taken care of by the datasheet. If there are many fields for each record, a datasheet may not be suitable. Therefore, you can display one record at a time in one view but allow the user to navigate to the next or the previous record. This can be done by creating appropriate menu items or by adding navigation buttons on the form. You can position such buttons in the bottom section of the form as follows:

Record Navigation Using Buttons

In this case, to navigate among records, the user can click the appropriate button.

To implement any other these scenarios, you must appropriately bind the database fields and records to the desired control(s).

Datasheet Binding

 

Introduction

Although you will create your databases as SQL files, to provide a friendlier environment, you create forms and other graphical accessories that allow the users to access and view information. In Visual Studio, you would create a Windows application.

ADO.NET provides various ways to connect to a database, the simplest consists of using a SqlDataAdapter variable because this gives you direct access to the SQL Server database. With other techniques, or as traditionally done, you would have to create an ODBC Data Source. The Microsoft Visual Studio .NET programming environment is so close to SQL Server that you can create and manage a database as if you were using a single application to take care of me.

Practical LearningPractical Learning: Creating a Data Source

  1. Start Microsoft Visual Studio .NET
  2. From the Server Explorer window, expand the Servers node. Expand the server that has your SQL Server installation or the database you want to use. Then expand the SQL Servers node
  3. Expand the name of the server and expand the CIC1 database that was created in the previous lesson
  4. Right-click the Tables node and click New Table
  5. Fill the table with the following fields:
     
    Column Name Data Type Length All Nulls
    EmployeeID int    
    EmployeeNo char 6 Cleared
    DateHired smalldatetime    
    FirstName varchar 20  
    LastName varchar 20 Cleared
    Salary smallmoney    
    IsMarried bit    
  6. Right-click EmployeeID and click Set Primary Key
  7. While EmployeeID still has focus, in the lower part of the table, set its Identity to Yes
  8. Save the table as Employees and close it
  9. From Server Explorer, under the Tables node of CIC1, double-click Employees
  10. Fill it up with the following fields:
     
    EmployeeNo DateHired FirstName LastName Salary IsMarried
    43-957 02/14/02 Albert Samson 16.58 1
    68-205 02/14/02 Leslie Ellison 16.24 0
    14-528 02/25/02 Elias Hawkins 14.26 0
    92-253 03/02/02 Anselme Wagner 22.05 1
    68-828 05/18/02 James Haught 24.32 1
  11. Close the table
  12. Start a new Windows Application named CIC2
  13. Change the Text of the form to Clarksville Ice Cream
  14. Set its StartPosition to CenterScreen
  15. On the main menu, click File -> New -> File...
  16. In the New File dialog box, click Icon File (.ico) and click Open
  17. Design the icon as follows:

  18. Right-click an empty area in the design section, position the mouse on Current Icon Image Types, and click 16x16, 16 colors
  19. Design the 16x16 version of the icon as follows:
     
  20. To save the icon, on the main menu, click File -> Save Icon1 As...
  21. Set the name to cic and note where (the folder) you are saving the icon
  22. Close the icon tab
  23. While the form is selected, in the Properties window, click the Icon field and click its ellipsis button. Then select the above cic.ico and click Open
  24. In the Properties window, click the Icon field and click its ellipsis button. Then select the App.ico and click Open
  25. To add another form to the project, on the main menu, click Project -> Add Windows Form...
  26. In the Name text box, replace the string with Employees and click Open
  27. Change its Icon to the above cic.ico and change its Text to Clarksville Ice Cream
  28. Set its ShowInTaskbar property to False and its StartPosition to CenterScreen
  29. From the Server Explorer, under the Tables node of CIC1, drag the Employees table and drop it on the Employees form

Introduction to Data Sets

The information stored in an object such as a database table is called a set, which is simply the group of its records. Because information is in fact referred to as data, the group of records in a table is also called a data set. To identify and manage the set of records, or data set, of a table, the Microsoft .NET Framework provides a class called DataSet.

After getting a connection to a database, you are ready to process it. You can use the data directly or, to better manage it, you can pass it to DataSet variable.

Practical LearningPractical Learning: Creating a Data Set

  1. On the main menu, click Data -> Generate Dataset...
  2. Click the text box to the right of the New radio button and change it to dsIceCream
     
  3. Click OK
 

The DataGrid Control

Data of a table is represented as a series of columns and rows. The columns are horizontal and hold the categories of information. A row contains entries various columns. The intersection of a column and a row is called a cell. The group of values stored in cells of a particular row is called a record.

To display information of a table, in the Server Explorer, you can just double-click it.

Instead of using a table to display data in cells, the Microsoft .NET Framework provides a control called DataGrid. This object also organizes its information in series of columns and rows whose intersections, called cells, hold the data of a table. To use a DataGrid, click it in the Toolbox and click the form. After doing this, you can specify that the information that displays in the cells will come from a DataSet. To do this, you can set the DataSource property of the DataGrid to the DataSet you have created. To actually display data in the DataGrid, you can call the Fill() method of the data adapter you are using.

Practical LearningPractical Learning: Displaying Data in a Data Grid

  1. On the Windows Forms section of the Toolbox, click DataGrid and click the form
  2. Position the DataGrid to the top-left corner and enlarge it
  3. Right-click the DataGrid and click Auto Format
  4. In the Formats list, click Colorful 3
     
    Auto Format
  5. Click OK
  6. On the form, click the DataGrid to make sure it is selected. In the Properties windows, change the following properties:
    CaptionText: Employees Records
    DataSource: dsCompAssets1.Employees
    Anchor: Top, Bottom, Left, Right
     
    Data Grid
  7. Double-click an unoccupied area of the form to generate its Load event
  8. Implement it as follows:
     
    Private Sub Employees_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.SqlDataAdapter1.Fill(Me.DsIceCream1)
     End Sub
  9. Display the first form, Form1.vb [Design]
  10. On the Toolbox, click Button and click the top-left section of the form
  11. Change the new button's Text to Employees and change its Name to btnEmployees
  12. Double-click the new Employees button and implement its Click event as follows:
     
    Private Sub btnEmployees_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmployees.Click
            Dim frmEmpl As Employees = New Employees
            frmEmpl.ShowDialog()
    End Sub
  13. Press Ctrl + F5 to execute the application
  14. On the first form, click the Employees button
     
    Data grid
  15. After viewing the form, close it and close the main form
 

Previous Copyright © 2004-2010 FunctionX, Inc. Next