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.

kim jingyuPosted May 27, 2016, 1:26 AM
good
Azure DamsPosted Apr 2, 2013, 8:25 AM
Mahesh I just have to say thanks. Thanks a lot it was so helpfull, i am learning how connect to a database and this help me to understand how i have to use the datagrid :D
Eduardo OjedaPosted Jun 29, 2012, 4:40 PM
Hello Mahesh!!! Great work !!!, it has helped me a lot to learn how to bind data to the DataGrid; is there a "Part 2:" where I could see how to bind an array to the DataGrid? Best regards, Eduardo
subrat nayakPosted Feb 24, 2011, 11:04 PM
if i have one combo box and one textbox and the data items of the combobox is to be selected fromthe database according to the data of the textbox
nirav sahayataeditedPosted May 11, 2010, 6:37 AMEdited May 11, 2010, 6:39 AM
sir, i want to bind datagrid with the form data entered by user at run time(without using database). how can i do this? thanks..
Darshan UnadkatPosted Jan 12, 2010, 9:35 AM
Hello Mahesh is der any way to append any data table or dataset in already created datagrid view with columns??????
inti manPosted Dec 22, 2009, 5:25 PM
gracias :D
Sweta LotlikarPosted Oct 24, 2009, 12:12 PM
Thanks:)
manneyPosted Jun 16, 2009, 7:49 AM
Can you bind datagridview to a result set from a data access layer?
thiru gnanamPosted Jun 12, 2009, 11:51 AM
xcvcx
thiru gnanamPosted Jun 12, 2009, 11:50 AM
Very Good
menaka mugundhunPosted Nov 20, 2008, 3:18 PM
hi, i am looking for displaying master detail data in a gridview..is there any option? or do you have any suggestions on how to achieve it? i would greatly appreciate your reply
Arbey Dario Munoz GomezPosted Apr 9, 2007, 5:15 PM
hi, i need your help, i need to show my datatable in the datagrid, but i want to show each column with a diferent width, for example my id is too short, but an adress for example, is too long, how i can do it??? thanks for your help
Alex BPosted Apr 6, 2007, 2:02 PM
How do I do Double Click Event to trap grid?
Mahesh ChandPosted Apr 5, 2007, 11:38 AM
Search this site using Search option at the top for delete a row in DataGrid.
sanke raghavendraPosted Mar 30, 2007, 7:04 AM
when i press the button i want delete a row in datagrid in window application
adnan ahmadPosted Jan 26, 2007, 10:21 PM
when we enter the new record in table to add command with dtatabinding then new record is add but when executed again ,the new record is removed so how we used save command with databinding.