6.10. The DataGrid Control

The DataGrid control supports the display, entry, and selection of data in table format, with row and column access. This section shows a simple use of DataGrid to display database and XML data. In Figure 6.15, for example, a data grid is used to display the Access database food description table we worked with in Section 5.10.

Figure 6.15. The Default DataGrid View of an Access Database File


DataGrid is a drag-and-drop control of the Windows Forms designer. We simply drop the grid on the form, then position and size it. Our work involves getting the data into the grid. Typically, this is a two-step process:

1.
We retrieve the data from its storage location, copying it into a DataSet object. We'll look at two examples: selecting data from an Access database and reading data from an XML file.

2.
We set the DataSource property of our grid to the DataSet table we wish to display. The formatting of the data within the grid is automatic.

The data grid needs to be populated before the form is displayed to the user. I chose to do that within the Form1 constructor, encapsulated within a member function I called initDataGrid()—for example,

public Form1()
{
   InitializeComponent();
   initDataGrid();
}

InitializeComponent() is a function generated by the Windows Forms designer. It allocates and sets the properties of both the form and the controls dropped on it through our design session. Our code, if any, is placed after that call.

The opening, selection, and retrieval of data within initDataGrid() is implemented by the same code sequence as that covered in Section 5.10, up to the filling of the DataSet object:

private void initDataGrid()
{
    string connect = "Provider=Microsoft.JET.OLEDB.4.0;" +
         @"data source=C:
utritiondatabaseFOOD_DES.mdb";

    OleDbConnection db_conn  = new OleDbConnection(connect);
    OleDbDataAdapter adapter = new OleDbDataAdapter();

    string command = "SELECT * FROM FOOD_DES";
    adapter.SelectCommand = new OleDbCommand(command, db_conn);

    DataSet ds = new DataSet();
    adapter.Fill( ds, "FOOD_DES" );

    dataGrid1.DataSource = ds.Tables["FOOD_DES"].DefaultView;
    db_conn.Close();
}

To display the data as rows and columns within the DataGrid object, we must set DataGrid's DataSource property to our DataSet object, and its DataMember property to the table within the DataSet that we wish to display. The following two lines do that explicitly:

dataGrid1.DataSource = ds;
dataGrid1.DataMember = "Food_DES";

Two shortcuts accomplish the same in a single assignment:

dataGrid1.DataSource = ds.Tables["Food_DES"].DefaultView;
dataGrid1.SetDataBinding( ds, "Food_DES" );

The following code fragment illustrates a second common data source for our control: an XML file (see Section 5.11 for a discussion of XML processing).

The two-step process is the same: We retrieve our data into one or more table formats, then set the DataGrid's DataSource to the table we wish to display. Figure 6.16 illustrates the resulting form:

Figure 6.16. The Default DataGrid View of an XML File


DataSet ds = new DataSet();
ds.ReadXml( @"C:C#Programsfood.xml"; );
dataGrid1.DataSource = ds.Tables[0].DefaultView;

We look at the DataGrid control again in Chapter 7, which focuses on ASP.NET.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.12.140