Merge two or more datatables in asp.net using C# and VB.net - Free Asp.Net,MVC,AngularJs,Jquery,Javascript,Sql Server,WCF,Entity framework snippets and tutorial

Merge two or more datatables in asp.net using C# and VB.net

In this article I am going to explain how to merge two or more datatables in c# using C# and vb.net
Merge two or more datatables in asp.net using C# and VB.net


Description:
Merge method is use to merge 2 datatables objects. 

Implementation:

HTML Markup of webform:
   <table>
    <tr>
    <td> <asp:GridView ID="GridView1" runat="server"
            CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView></td> <td></td><td>   <asp:GridView ID="GridView2" runat="server"
            CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView></td>
       <td></td>
       <td> <asp:GridView ID="GridView3" runat="server"
            CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView></td>
    </tr>
    </table>      
    
        <asp:GridView ID="GridView4" runat="server"
            CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
        </asp:GridView>

Import the namespce:
C# code:
using System.Data;

VB.net code:
Imports System.Data

Write the below code on code file:
C# code:

protected void Page_Load(object sender, EventArgs e)
    {
        BindGrid1();
        BindGrid2();
        BindGrid3();
        BindGrid4();
    }
    public DataTable DatatableOne()
    {
        DataTable dt = new DataTable();
        var colm = dt.Columns.Add("ID", typeof(int));
        dt.PrimaryKey = new DataColumn[] { colm };
        dt.Columns.Add("Name", typeof(string));
        dt.Columns.Add("Class", typeof(string));
        dt.Rows.Add(1, "John","12th");
        return dt;
    }

    public DataTable DatatableTwo()
    {
        DataTable dt = new DataTable();
        var colm = dt.Columns.Add("ID", typeof(int));
        dt.PrimaryKey = new DataColumn[] { colm };
        dt.Columns.Add("Roll Number", typeof(int));
        dt.Rows.Add(1, "123654");
        return dt;
    }
    public DataTable DatatableThree()
    {
        DataTable dt = new DataTable();
        var colm = dt.Columns.Add("ID", typeof(int));
        dt.PrimaryKey = new DataColumn[] { colm };
        dt.Columns.Add("Marks", typeof(int));
        dt.Rows.Add(1, "400");
        return dt;
    }
    public void BindGrid1()
    {
        GridView1.DataSource = DatatableOne();
        GridView1.DataBind();
    }
    public void BindGrid2()
    {
        GridView2.DataSource = DatatableTwo();
        GridView2.DataBind();
    }
    public void BindGrid3()
    {
        GridView3.DataSource = DatatableThree();
        GridView3.DataBind();
    }
    public DataTable MergeTables()
    {
        var tbl = DatatableOne();
        tbl.Merge(DatatableTwo());
        tbl.Merge(DatatableThree());
        return tbl;
    }
    public void BindGrid4()
    {
        GridView4.DataSource = MergeTables();
        GridView4.DataBind();
    }

VB.net code:
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        BindGrid1()
        BindGrid2()
        BindGrid3()
        BindGrid4()
    End Sub
    Public Function DatatableOne() As DataTable
        Dim dt As New DataTable()
        Dim colm = dt.Columns.Add("ID", GetType(Integer))
        dt.PrimaryKey = New DataColumn() {colm}
        dt.Columns.Add("Name", GetType(String))
        dt.Columns.Add("Class", GetType(String))
        dt.Rows.Add(1, "John", "12th")
        Return dt
    End Function
    Public Function DatatableTwo() As DataTable
        Dim dt As New DataTable()
        Dim colm = dt.Columns.Add("ID", GetType(Integer))
        dt.PrimaryKey = New DataColumn() {colm}
        dt.Columns.Add("Roll Number", GetType(Integer))
        dt.Rows.Add(1, "123654")
        Return dt
    End Function
    Public Function DatatableThree() As DataTable
        Dim dt As New DataTable()
        Dim colm = dt.Columns.Add("ID", GetType(Integer))
        dt.PrimaryKey = New DataColumn() {colm}
        dt.Columns.Add("Marks", GetType(Integer))
        dt.Rows.Add(1, "400")
        Return dt
    End Function
    Public Sub BindGrid1()
        GridView1.DataSource = DatatableOne()
        GridView1.DataBind()
    End Sub
    Public Sub BindGrid2()
        GridView2.DataSource = DatatableTwo()
        GridView2.DataBind()
    End Sub
    Public Sub BindGrid3()
        GridView3.DataSource = DatatableTwo()
        GridView3.DataBind()
    End Sub
    Public Function MergeTable() As DataTable
        Dim tbl = DatatableOne()
        tbl.Merge(DatatableTwo())
        tbl.Merge(DatatableThree())
        Return tbl
    End Function
    Public Sub BindGrid4()
        GridView4.DataSource = MergeTable()
        GridView4.DataBind()
    End Sub

Build and run the created  application.


  In this article we have learn how to merge two or more datatables in asp.net using C# and Vb.net. I hope you enjoyed this article. Please post you comment, query and feedback about this article. You can like me on Facebook, Google+, Linkedin and Twitter via hit on Follow us Button and also can get update follow by Email.  

What do you think about this article?

If you found this article useful, please share and follow on Facebook, Twitter, Google Plus and other social media websites. To get free updates subscribe to newsletter. Please put your thoughts and feedback in comments section.

Share this

Share on FacebookTweet on TwitterPlus on Google+


EmoticonEmoticon