Tuesday, March 9, 2010

How to merge the data of two table in a DataGrid

or showing the data of two table in a dataGrid make sure that the field name and field type of both table should be same. 

The aspx code is: <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Merge Two DataSet</title>
    <form id="form1" runat="server">
      <table width="70%" align="center" border="4">
        <tr><td height="20px"></td></tr>
        <tr><td align="center">
               <asp:DataGrid ID="GridAllRecord" runat="server" HeaderStyle-BackColor="blue">
         <tr><td height="20px"></td></tr>
</html>The aspx.cs code

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
    SqlConnection con;
    SqlDataAdapter da;  
    DataSet ds1 = new DataSet();
    DataSet ds2 = new DataSet();
    protected void Page_Load(object sender, EventArgs e)

   public void binddata()
       con=new SqlConnection("Data Source=MCN101; Initial Catalog=MergeTable; Uid=sa; pwd=");
       da = new SqlDataAdapter("Select * from Table1", con);
       da.Fill(ds1, "Record");
       da = new SqlDataAdapter("select * from Table2", con);
       da.Fill(ds2, "Record");
       GridAllRecord.DataSource = ds1;