How to Merge Two Datatable or Dataset Values Bind to Gridview Using Asp.Net C#

Merge Two Datatable or Dataset Values to Gridview


Merge Two Datatable or Dataset Values Bind to GridView or Some Controls Using in Asp.Net C#.

                                   DEMO



                                      Download
  
                                   HTML Coding


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to Merge Two Datatable or Dataset Values  Bind to  Gridview  Using Asp.Net C#</title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    <table><tr><td>
        &nbsp;</td><td>
            &nbsp;</td></tr>
        </table>
         <table><tr><td>
             <asp:GridView ID="GridView1" runat="server">
             </asp:GridView>
             </td></tr></table>
    </div>
    </form>
</body>
</html>



                                 C# Coding


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {  
        GridView1.Attributes.Add("bordercolor","red");
        MergeGrid();
    }
    protected void MergeGrid()
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbcon"].ToString());
        con.Open();
        SqlDataAdapter adp = new SqlDataAdapter("select * from Reg", con);
        DataSet ds1 = new DataSet();
        adp.Fill(ds1);     
        adp = new SqlDataAdapter("select * from sale", con);
        DataSet ds2 = new DataSet();
        adp.Fill(ds2);      
        ds1.Merge(ds2,true);    
        if (ds1.Tables[0].Rows.Count > 0 && ds2.Tables[0].Rows.Count>0)
        {
            GridView1.DataSource = ds1;
            GridView1.DataBind();
        }
        con.Close();
    }
   

}


Create Two Database tables

              

                              



Next - Add New Webforms - Select GridView From 

Toolbox





Next - Select Two Database table to Two DataSet & Merge & Bind Gridview






1 comments:

Post a Comment