-->

Tuesday, February 3, 2015

How to bind Gridview from two Merged Data Table

How to bind Gridview from two Merged Data Table

Introduction

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

datatable1.Merge(datatable2);

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

   GridView1.DataSource = datatable1;
        GridView1.DataBind();

Now code Generate the following output:

How to bind Gridview from two Merged Data Table

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved