如何在数据集中合并2个动态数据表,如下图所示 [英] How to Merge 2 Dynamic data tables in a dataset as shown below image

查看:73
本文介绍了如何在数据集中合并2个动态数据表,如下图所示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.OleDb;


public partial class _Default : System.Web.UI.Page 
{
    OleDbConnection cn;
    DataSet ds;
    OleDbDataAdapter oda1,oda2;
    DataTable dt1,dt2,dt3;
    DataRow dr;
    DataColumn c1, c2, c3,c4,c5,c6;

    protected void Page_Load(object sender, EventArgs e)
    {
        cn = new OleDbConnection("provider = Microsoft.Jet.OLEDB.4.0; data source = c:\\db1.mdb");
        cn.Open();
        ds = new DataSet();
        oda1 = new OleDbDataAdapter("select * from emp", cn);
        oda1.Fill(ds, "emp");
        oda2 = new OleDbDataAdapter("select * from dept", cn);
        oda2.Fill(ds, "dept");


    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        GridView1.DataSource = ds.Tables[0].DefaultView;
        GridView2.DataSource = ds.Tables[1].DefaultView;
        GridView1.DataBind();
        GridView2.DataBind();
        GridView1.Visible = true;
        GridView2.Visible = true;

    }
    protected void Button2_Click(object sender, EventArgs e)
    {

        dt1 = new DataTable();
        dt1.TableName = "T1";

        dt2 = new DataTable("T2");
        dt3 = new DataTable("T3");

 
        c1 = new DataColumn("eName");
        c2 = new DataColumn("eId");
        c3 = new DataColumn("Did");
        c4 = new DataColumn("Did");
        c5 = new DataColumn("dName");
        c6 = new DataColumn("dAddress");

        dt1.Columns.Add(c1); dt1.Columns.Add(c2);
        dt1.Columns.Add(c3);

        dt2.Columns.Add(c4);
        dt2.Columns.Add(c5);
        dt2.Columns.Add(c6);

        for (int i = 0; i < 5; i++)
        {
            dr = dt1.NewRow();

            dr[0] = "a" + i;
            dr[1] = "b" + i;
            dr[2] = "c" + i;
            
            dt1.Rows.Add(dr);
            
        }

        ds.Tables.Add(dt1);
        


        for (int i = 0; i < 5; i++)
        {
            dr = dt2.NewRow();

            dr[0] = "c" + i;
            dr[1] = "x" + i;
            dr[2] = "y" + i;

            dt2.Rows.Add(dr);

        }

        ds.Tables.Add(dt2);

        GridView1.DataSource = ds.Tables["T1"].DefaultView;
        GridView1.DataBind();
        GridView2.DataSource = ds.Tables["T2"].DefaultView;
        GridView2.DataBind();
        GridView1.Visible = true;
        GridView2.Visible = true;

        dt1.Merge(dt2, false, MissingSchemaAction.AddWithKey);

        GridView3.DataSource = dt1;
        GridView3.DataBind();
        GridView3.Visible = true;

    }
    protected void Button2_Click1(object sender, EventArgs e)
    {
        
    }
}



我需要将这个程序放下,如下图所示
在此处查看图像或http://www.freeimagehosting.net/uploads/a93c18e8eb.jpg

当iam创建2个动态表,其中1个公共列中加载了动态数据时,iam合并这2个表时
输出就像链接中的图像
请帮我解决这个问题

预先感谢一百万.



i need out put of this programe as shown in image below
See here for image or http://www.freeimagehosting.net/uploads/a93c18e8eb.jpg

When iam creating 2 dynamic tables with 1 common column loaded with dynamic data in it and when iam merging this 2 tables
the out put is like the image in the link
please do help me solving this problem

thanks a million in advance

推荐答案

iam试图制作2个动态表,并希望使用公用列"did"合并它们,但输出不明确

iam trying to make 2 dynamic tables and want to merge them using common column "did" but out put is not clear

  emp                                       dept
eno ename did                       did dname daddress
1    sa    10                        10  IT    Hyd
2    sam   20                        20  Phy   Hyd
3    ram   30                        30  Bio   Hyd

now i need out put as 

                         comb
             eno ename did dname daddress
             1    sa    10  IT    Hyd
             2    sam   20  Phy   Hyd
             3    ram   30  Bio   Hyd

but out put is coming like this 

                       comb
             eno ename did dname daddress
             1    sa    10   
             2    sam   20   
             3    ram   30   
                        10  IT    Hyd
                        20  Phy   Hyd
                        30  Bio   Hyd



and the code is mentioned above 
is there any coding or solution to get the desired output 


有什么帮助,请那里的任何人来帮助我/............
any help please any one there to help me /............


这篇关于如何在数据集中合并2个动态数据表,如下图所示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆