How to bind Gridview from two Merged Data Table

February 03, 2015 0 Comments


It is a combination of DataColumns and DataRow. Generally, we use it for data binding purpose. In this article we will learn how to design the DataTable , merge two DataTable and Bind the gridview control with merged DataTable. First of all, create the object of it then add some column in it. After define the columns you will enter data in the columns. We have already dicussed on this topic earlier. Today, we will focus on merge( ) method.

Add columns and rows in the DataTable (Copy and Paste)

DataTable datatable1 = new DataTable();

        datatable1.Columns.Add("id", typeof(Int32));
        datatable1.Columns.Add("Name", typeof(string));
        datatable1.Rows.Add(1, "A");
        datatable1.Rows.Add(2, "B");

Above code define, we have two column that is, id with integer DataType and Name with string DataType. Similarly, we can design code for second DataTable. Like

  DataTable datatable2 = new DataTable();
        datatable2.Columns.Add("id", typeof(Int32));
        datatable2.Columns.Add("Name", typeof(string));
        datatable2.Rows.Add(3, "C");
        datatable2.Rows.Add(4, "D");

How to merge  DataTable:

After preparing the DataTable, you should use Merge( ) method. Like


Now, you have to bind the GridView with Merged DataTable.

   GridView1.DataSource = datatable1;

Now code Generate the following output:

How to bind Gridview from two Merged Data Table

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google