When it comes to data-bound controls and database programming, DataGrid control is the most versatile, powerful and flexible control available in Visual Studio .NET. Using DataGrid control, you can write full-fledged database application in matter of minutes and hours.
Using DataSource property of a DataGrid control, you can fill various kinds of data in a DataGrid including data from a DataSet, DataView, DataViewManger, Arrays, Lists, IList and so on. In a series of articles, I will discuss various features of DataGrid control and data binding in DataGrid.
First two articles of this series will discuss data-binding features. In next articles, I will work on DataGrid customization.
In this article, I will show you different ways to fill data in a DataGrid. Some of them are DataTable, DataView, DataSet and DataViewManager. In Part 2 of this series, I will show you how to fill data from arrays, IList, IListSource and other data sources.
About the Application
This application is a Windows application. I add a DataGrid control and some buttons on the form as you can see from Figure 1. To make this article short, I will discuss only DataView, DataViewManager, DataSet and DataTable options.
As you can see from Figure 1, I have one button for each data source. Clicking on DataTable button will fill data in DataGrid from a DataTable, clicking on DataView button will fill data from a DataView to DataGrid and so on.
Figure 1.
Source Code
In this application, I am adding data programmatically. I am not accessing data from any database. Perhaps you may want to access data from a data source. Method is same. In this sample, I create two DataTable objects A and B programmatically. The source code listed in Listing 1 shows CreateDataTableA and DataTableB methods, which create table A and B.
As you can see from Listing 1, the DataTable class is used to create a table. DataColumn class is used to create columns to the table and DataRow class is used to add rows to a data table.
Listing 1. Creating DataTable A and B.
// Create DataTable A
private DataTable CreateDataTableA()
{
DataTable aTable = new DataTable("A");
DataColumn dtCol;
DataRow dtRow;
// Create ID column and add to the DataTable.
dtCol = new DataColumn();
dtCol.DataType= System.Type.GetType("System.Int32");
dtCol.ColumnName = "ID";
dtCol.AutoIncrement = true;
dtCol.Caption = "ID";
dtCol.ReadOnly = true;
dtCol.Unique = true;
// Add the column to the DataColumnCollection.
aTable.Columns.Add(dtCol);
// Create Name column and add to the table
dtCol = new DataColumn();
dtCol.DataType= System.Type.GetType("System.String");
dtCol.ColumnName = "FName";
dtCol.AutoIncrement = false;
dtCol.Caption = "First Name";
dtCol.ReadOnly = false;
dtCol.Unique = false;
aTable.Columns.Add(dtCol);
// Create Last Name column and add to the table.
dtCol = new DataColumn();
dtCol.DataType= System.Type.GetType("System.String");
dtCol.ColumnName = "LName";
dtCol.AutoIncrement = false;
dtCol.Caption = "Last Name";
dtCol.ReadOnly = false;
dtCol.Unique = false;
aTable.Columns.Add(dtCol);
// Create three rows to the table
dtRow = aTable.NewRow();
dtRow["ID"] = 1001;
dtRow["FName"] = "Mahesh";
dtRow["LName"] = "Chand" ;
aTable.Rows.Add(dtRow);
dtRow = aTable.NewRow();
dtRow["ID"] = 1002;
dtRow["FName"] = "Melanie";
dtRow["LName"] = "Talmadge" ;
aTable.Rows.Add(dtRow);
dtRow = aTable.NewRow();
dtRow["ID"] = 1003;
dtRow["FName"] = "Vinay";
dtRow["LName"] = "Bansal" ;
aTable.Rows.Add(dtRow);
return aTable;
}
// Create DataTable B
private DataTable CreateDataTableB()
{
DataTable bTable = new DataTable("B");
DataColumn dtCol;
DataRow dtRow;
// Create ID column and add to the DataTable.
dtCol = new DataColumn();
dtCol.DataType= System.Type.GetType("System.Int32");
dtCol.ColumnName = "CustomerID";
dtCol.AutoIncrement = true;
dtCol.Caption = "CustomerID";
dtCol.ReadOnly = true;
dtCol.Unique = true;
// Add the column to the DataColumnCollection.
bTable.Columns.Add(dtCol);
// Create Name column and add to the table
dtCol = new DataColumn();
dtCol.DataType= System.Type.GetType("System.String");
dtCol.ColumnName = "Name";
dtCol.AutoIncrement = false;
dtCol.Caption = "Name";
dtCol.ReadOnly = false;
dtCol.Unique = false;
bTable.Columns.Add(dtCol);
// Create Last Name column and add to the table.
dtCol = new DataColumn();
dtCol.DataType= System.Type.GetType("System.String");
dtCol.ColumnName = "Address";
dtCol.AutoIncrement = false;
dtCol.Caption = "Address";
dtCol.ReadOnly = false;
dtCol.Unique = false;
bTable.Columns.Add(dtCol);
// Create three rows to the table
dtRow = bTable.NewRow();
dtRow["CustomerID"] = 11;
dtRow["Name"] = "Mr. Peter Ferrera";
dtRow["Address"] = "9th Street, Bank Road, NOIDA" ;
bTable.Rows.Add(dtRow);
dtRow = bTable.NewRow();
dtRow["CustomerID"] = 21;
dtRow["Name"] = "Ross Tomo";
dtRow["Address"] = "North Street, Parkway Road, SunCity" ;
bTable.Rows.Add(dtRow);
dtRow = bTable.NewRow();
dtRow["CustomerID"] = 31;
dtRow["Name"] = "Dr. Jog Rocky";
dtRow["Address"] = "Turnpike Avenue, Philly" ;
bTable.Rows.Add(dtRow);
return bTable;
}
Understanding Listing 1 is important because it shows the data source for our application. We will generate DataSet, DataView and DataViewManager from DataTable objects. Ok, first let's see how to view data from a DataTable in a DataGrid.
To view a DataTable data in a DataGrid, just set DataSource property of DataGrid as DataTable object name. As you can see from Listing 2, I call CreateDataTableA method, which returns a DataTable. After that I set DataGrid.DataSource as DataTable A. This code is written on DataTable button click event handler.
Listing 2. Binding a DataTable to a DataGrid.
private void DataTableBtn_Click(object sender, System.EventArgs e)
{
// Create a DataTable and Bind it to the DataGrid
DataTable A = CreateDataTableA();
dataGrid1.DataSource = A;
}
The output of DataTable button click looks like Figure 2.
Figure 2. Viewing data from a DataTable in a DataGrid.
Generating a DataView from a DataTable is pretty easy. You create a DataView object using DataView class constructor and pass a DataTable as default parameter. As you can see from Listing 3, I call CreateDataTableB, which returns a DataTable object. After that I call DataView contructor and pass DataTable as default parameter and set DataView as DataGrid's DataSource.
Listing 3. Binding a DataView to a DataGrid.
private void DataViewBtn_Click(object sender, System.EventArgs e)
{
// Create a DataView using Table B
DataTable table = CreateDataTableB();
DataView view = new DataView(table);
// Bind DataView to the DataGrid
dataGrid1.DataSource = view;
}
The DataView button click hosts the code listed in Listing 3. If you click on DataView button, the output looks like Figure 3.
Figure 3. Viewing data from a DataView in a DataGrid.
A DataSet is a set of DataTable objects. You can bind a DataSet to a DataGrid by using DataGrid's DataSource or SetDataBinding method. The code listed in Listing 4 adds two DataTable objects to a DataSet and call DataGrid's SetDataBinding method to bind a DataSet to a DataGrid.
Listing 4. Binding a DataSet to a DataGrid
private void DataSetBtn_Click(object sender, System.EventArgs e)
{
// Create a DataSet & add DataTable A and B to it
DataTable table = CreateDataTableA();
DataSet ds = new DataSet("ABSet");
ds.Tables.Add(table);
table = CreateDataTableB();
ds.Tables.Add(table);
// Bind DataSet to the DataGrid
dataGrid1.SetDataBinding(ds,"B");
}
The output of DataSet button click, which executes code listed in Listing 4 looks like Figure 4.
Figure 4. Viewing data from a DataSet in a DataGrid.
A DataViewManager represents all DataTables of a DataSet. You can add DataTable objects to a DataSet by calling it's Tables.Add method. The source code listed in Listing 5 creates a DataViewManager by using DataSet as default parameter. After that I set DataGrid's DataSource property as DataViewManager.
Listing 5. Binding a DataViewManager to DataGrid
private void DataViewManagerBtn_Click(object sender, System.EventArgs e)
{
// Create a DataSet
DataTable table = CreateDataTableA();
DataSet ds = new DataSet("ABSet");
ds.Tables.Add(table);
table = CreateDataTableB();
ds.Tables.Add(table);
// Create a DataViewManager
DataViewManager dvm = new DataViewManager(ds);
// Bind DataViewManager to the DataGrid
dataGrid1.DataSource = dvm;
}
The DataViewManager shows all DataTable objects available in a DataSet. The Figure 5 shows both A and B data tables available in DataGrid.
Figure 5. Binding DataViewManager to a DataGrid,
If you click on a table, you can see it's data as you see in Figure 6.
Figure 6.