ASP.NET: Merge two data-tables into one in asp.net using c# & vb.net.

Aman Sharma
0
Some time while developing application in asp.net , it is required to merge two datatables. It can be achieved by using Merge function of Datatable in asp.net.

In this article I will explain how to merge two data tables in to one.

Public DataTable merge_table()
{
Var tbl = GetTable1();
tbl.Merge(GetTable2());
return tbl;
}


Demo:
Click on image to Enlarge it


Implementation:

 Now design your web page:
   <div><table>
        <tr><td ><b>First Table<br />
            </b></td>
            <td >&nbsp;</td>
            <td><b>Second table</b></td>
            <td></td></tr>
        <tr><td >
            <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None">         
          
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />          
           
            </asp:GridView></td><td >
                &nbsp; &nbsp;</td><td> <asp:GridView ID="GridView2" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" style="margin-left: 0px">
               
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />               
                </asp:GridView></td></tr>
        <tr><td ></td><td >&nbsp;</td><td></td></tr>
        <tr><td  colspan="3"><strong>
            <br />
            Result Table<br />
            </strong></td></tr>
        <tr><td colspan="3"> <asp:GridView ID="ResultGridview" runat="server" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
          
            <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />           
            </asp:GridView></td></tr>
         </table>
       
      
      
    </div>

Asp.NET Code: Now write following code to Bind Form View Data Control.
In C#:
Add Following Namespaces:

Using System.Data;

Now write following code to merge Two datatable:

public partial class dataTable : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Fill_Grid1();
        Fill_Grid2();
       FillResultGrid();
    }
public DataTable GetTable2()
{
    DataTable table = new DataTable();
    var colm= table.Columns.Add("ID", typeof(int));
    table.PrimaryKey = new DataColumn[] {colm };
    table.Columns.Add("Name", typeof(string));
    table.Rows.Add(1, "Vijay");
    table.Rows.Add(2, "vikram");
    table.Rows.Add(3, "Vishal");
    table.Rows.Add(4, "Vikas");
    return table;
}
public DataTable GetTable1()
{
    DataTable table = new DataTable();
    var colm= table.Columns.Add("ID", typeof(int));
    table.PrimaryKey = new DataColumn[] {colm };
    table.Columns.Add("Name", typeof(string));
    table.Columns.Add("RollNo", typeof(string));
    table.Rows.Add(1, "Ajay","A01");
    table.Rows.Add(2, "Anil", "A02");
    table.Rows.Add(3, "Ankit", "B03");
    table.Rows.Add(4, "Anish", "B04");
    return table;
}
 public DataTable merge_table()
{
  var tbl = GetTable1();
  tbl.Merge(GetTable2());
  return tbl;
}
 public void Fill_Grid1()
 {
     GridView1.DataSource = GetTable1();
     GridView1.DataBind();
 }
public void Fill_Grid2()
{
    GridView2.DataSource = GetTable2();
    GridView2.DataBind();
}
public void FillResultGrid()
{
    ResultGridview.DataSource = merge_table();
    ResultGridview.DataBind();
}
}

In Vb.Net:

Add Following Namespaces:

Imports System.Data

Write following code:

Partial Public Class dataTable
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        Fill_Grid1()
        Fill_Grid2()
        FillResultGrid()
    End Sub
    Public Function GetTable2() As DataTable
        Dim table As New DataTable()
        Dim colm = table.Columns.Add("ID", GetType(Integer))
        table.PrimaryKey = New DataColumn() {colm}
        table.Columns.Add("Name", GetType(String))
        table.Rows.Add(1, "Vijay")
        table.Rows.Add(2, "vikram")
        table.Rows.Add(3, "Vishal")
        table.Rows.Add(4, "Vikas")
        Return table
    End Function
    Public Function GetTable1() As DataTable
        Dim table As New DataTable()
        Dim colm = table.Columns.Add("ID", GetType(Integer))
        table.PrimaryKey = New DataColumn() {colm}
        table.Columns.Add("Name", GetType(String))
        table.Columns.Add("RollNo", GetType(String))
        table.Rows.Add(1, "Ajay", "A01")
        table.Rows.Add(2, "Anil", "A02")
        table.Rows.Add(3, "Ankit", "B03")
        table.Rows.Add(4, "Anish", "B04")
        Return table
    End Function
    Public Function merge_table() As DataTable
        Dim tbl = GetTable1()
        tbl.Merge(GetTable2())
        Return tbl
    End Function
    Public Sub Fill_Grid1()
        GridView1.DataSource = GetTable1()
        GridView1.DataBind()
    End Sub
    Public Sub Fill_Grid2()
        GridView2.DataSource = GetTable2()
        GridView2.DataBind()
    End Sub
    Public Sub FillResultGrid()
        ResultGridview.DataSource = merge_table()
        ResultGridview.DataBind()
    End Sub
End Class

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !