如何正确地将此数据表合并到数据集中 [英] How to Merge this data tables in data set Correctly

查看:79
本文介绍了如何正确地将此数据表合并到数据集中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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;


public partial class _Default : System.Web.UI.Page 
{
    DataSet ds;
  
    DataTable dt1,dt2;
    DataRow r;
    DataColumn c1, c2, c3, c4, c5, c6;
    protected void Page_Load(object sender, EventArgs e)
    {
        ds = new DataSet("D-base");
        dt1 = new DataTable();
        dt2 = new DataTable();
      
        c1 = new DataColumn("empno");
        c2 = new DataColumn("empname");
        c3 = new DataColumn("depno");
        c4 = new DataColumn("depno");
        c5 = new DataColumn("depname");
        c6 = new DataColumn("depaddress");
        dt1.TableName = "emp";
        dt2.TableName = "dept";
        dt1.Columns.Add(c1); dt1.Columns.Add(c2); dt1.Columns.Add(c3);
        dt2.Columns.Add(c4); dt2.Columns.Add(c5); dt2.Columns.Add(c6);

       

        //dt1.PrimaryKey = dt1.Columns[0];
        //dt2.PrimaryKey = dt2.Columns[3];
       
    }

 
    protected void Showdt_btn_Click(object sender, EventArgs e)
    {

        for (int i = 0; i < 10; i++)
        {
            r = dt1.NewRow();
            r[c1] = i;
            r[c2] = "ramu" + i;
            r[c3] = 100 + i;
            dt1.Rows.Add(r);
        }
        ds.Tables.Add(dt1);

        GridView1.DataSource = ds.Tables[0].DefaultView;
        GridView1.DataBind();
        GridView1.Visible = true;
        for (int i = 0; i < 10; i++)
        {
            r = dt2.NewRow();
            r[c4] = 100 + i;
            r[c5] = "IT deptno " + i;
            r[c6] = "Hyd cir no" + i;
            dt2.Rows.Add(r);
        }
        ds.Tables.Add(dt2);
        GridView2.DataSource = ds.Tables[1].DefaultView;
        GridView2.DataBind();
        GridView1.Visible = true;

        dt1.Merge(dt2, false, MissingSchemaAction.AddWithKey);
        GridView3.DataSource = ds.Tables[0].DefaultView;
        GridView3.DataBind();
        GridView3.Visible = true;
    }



在输出中没有得到正确的格式

我希望它们之间正确存在空白值
有什么解决办法吗?



in the Out put it is not getting correct format

there are blank values in between i want them to be correct
any solution?

empno empname depno depname    depaddress 
0     ramu0   100     
1     ramu1   101     
2     ramu2   102     
3     ramu3   103     
4     ramu4   104     
5     ramu5   105     
6     ramu6   106     
7     ramu7   107     
8     ramu8  108     
9     ramu9  109     
            100    IT deptno 0  Hyd cir no0 
            101    IT deptno 1  Hyd cir no1 
            102    IT deptno 2  Hyd cir no2 
            103    IT deptno 3  Hyd cir no3 
            104    IT deptno 4  Hyd cir no4 
            105    IT deptno 5  Hyd cir no5 
            106    IT deptno 6  Hyd cir no6 
            107    IT deptno 7  Hyd cir no7 
            108    IT deptno 8  Hyd cir no8 
            109    IT deptno 9  Hyd cir no9 


在此输出中,我要消除空白值,并希望将以下值合并为空白,请告诉我如何?可以拍出轻巧的丁字裤


here in this output i want to eliminate blank values and want to merge values below into blank spaces please tell me how ? it is possible to make out put likle hits

empno empname depno depname      depaddress 
 0    ramu0   100   IT deptno 0  Hyd cir no0   
 1    ramu1   101   IT deptno 1  Hyd cir no1  
 2    ramu2   102   IT deptno 2  Hyd cir no2    
 3    ramu3   103   IT deptno 3  Hyd cir no3    
 4    ramu4   104   IT deptno 4  Hyd cir no4    
 5    ramu5   105   IT deptno 5  Hyd cir no5    
 6    ramu6   106   IT deptno 6  Hyd cir no6   
 7    ramu7   107   IT deptno 7  Hyd cir no7    
 8    ramu8   108   IT deptno 8  Hyd cir no8    
 9    ramu9   109   IT deptno 9  Hyd cir no9

推荐答案

做一件事情.创建两个表Employee和Department.将它们添加到数据集.
现在在它们之间创建Ralation
ds.Relations.Add("PersonPet",person.Columns ["Id"],pet.Columns ["OwnerId"]));
ds.Relations.Add("DeptEmp",Dept.Columns ["DeptId"],Emp.Columns ["DeptId"]);
现在遍历部门表并从Employee获取所有子行
foreach(Dept.Rows中的DataRow DeptRow)
{
Console.WriteLine("{0}-{1}",DeptRow ["DeptId"],DeptRow ["DeptName"]));

foreach(DeptRow.GetChildRows("DeptEmp")中的DataRow EmpRow)
{
Console.WriteLine("{0}-{1}",EmpRow ["EmpId"],petRow ["EmpName"]));
}
}
这是在编写控制台,您可以将值存储在表中并绑定到某些网格视图
Do One Thing. Create two tables Employee and Department. Add them to Data Set.
Now create Ralation between them
ds.Relations.Add("PersonPet",person.Columns["Id"], pet.Columns["OwnerId"]);
ds.Relations.Add("DeptEmp",Dept.Columns["DeptId"], Emp.Columns["DeptId"]);
Now Traverse Dept Table and get all child rows from Employee
foreach (DataRow DeptRow in Dept.Rows)
{
Console.WriteLine("{0} - {1}",DeptRow ["DeptId"], DeptRow["DeptName"]);

foreach (DataRow EmpRow in DeptRow.GetChildRows("DeptEmp"))
{
Console.WriteLine("{0} - {1}", EmpRow["EmpId"], petRow["EmpName"]);
}
}
Here is am writing to console, you can store the values in a table and bind to some grid view


这篇关于如何正确地将此数据表合并到数据集中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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