合并数据表不按行方式工作 [英] Merging Datatables not working row wise

查看:59
本文介绍了合并数据表不按行方式工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并数据表。但是我没有得到理想的结果。

I am trying to merge datatables. But I am not getting the desired result.

string USN = "4PS12MCA28";
string Name = "Partha";
string FName = "Krishnappa";
string CC = "P11MCA41";
string CTIT = "JAVA";
DataTable DT2 = new DataTable();
DataTable DT1 = new DataTable();
DT1.Columns.Add("USN");
DT1.Columns.Add("Name");
DT1.Columns.Add("FName");
DT2.Columns.Add("CC");
DT2.Columns.Add("C_Title");
DataRow DR = DT1.NewRow();
DR["USN"] = USN;
DR["Name"] = Name;
DR["FName"] = FName;
DT1.Rows.Add(DR);
DataRow DR1 = DT2.NewRow();
DR1["CC"] = CC;
DR1["C_Title"] = CTIT;
DT2.Rows.Add(DR1);
DT1.Merge(DT2);
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = DT1;
dataGridView1.ReadOnly = true



我得到的输出是 https://plus.google.com/photos/105587850868503389652/专辑/ 6157183279136814705/6157183287988706530< / a> [ ^ ]

我正在寻找的输出是:


The output which I am getting is https://plus.google.com/photos/105587850868503389652/albums/6157183279136814705/6157183287988706530</a>[^]
The output which I am loooking for is :

USN        |Name     |FName        |CC       |CTitle
4PS12MCA28  Partha    Krishnappa    P11MCA41  JAVA



请帮忙。谢谢

编辑:

我得到的o / p是:


Please help. Thank you
EDIT :
The o/p which I am getting is :

USN        |Name  |FName     |CC       |CTitle
4PS12MCA28 Partha Krishnappa
                              P11MCA41  JAVA



我确切地说我错了。谢谢。


I dono exactly where I am going wrong. Thank you.

推荐答案

我明白了!

这很痛苦 - 没有标准的方法可以做到这一点,除非两者之间有共同的信息。两张桌子。如果有的话,这是非常简单的JOIN东西。



但是有可能。添加此方法:

I see!
That's a pain - there is no standard way to do that unless there is common information between the two tables. If there is, it's pretty simple JOIN stuff.

But it is possible. Add this method:
private DataTable MergeColumns(DataTable dt1, DataTable dt2)
    {
    DataTable result = new DataTable();
    foreach (DataColumn dc in dt1.Columns)
        {
        result.Columns.Add(new DataColumn(dc.ColumnName, dc.DataType));
        }
    foreach (DataColumn dc in dt2.Columns)
        {
        result.Columns.Add(new DataColumn(dc.ColumnName, dc.DataType));
        }
    for (int i = 0; i < Math.Max(dt1.Rows.Count, dt2.Rows.Count); i++)
        {
        DataRow dr = result.NewRow();
        if (i < dt1.Rows.Count)
            {
            for (int c = 0; c < dt1.Columns.Count; c++)
                {
                dr[c] = dt1.Rows[i][c];
                }
            }
        if (i < dt2.Rows.Count)
            {
            for (int c = 0; c < dt2.Columns.Count; c++)
                {
                dr[dt1.Columns.Count + c] = dt2.Rows[i][c];
                }
            }
        result.Rows.Add(dr);
        }
    return result;
    }



然后只需在代码中使用它:


Then just use it in your code:

string USN = "4PS12MCA28";
string Name = "Partha";
string FName = "Krishnappa";
string CC = "P11MCA41";
string CTIT = "JAVA";
DataTable DT2 = new DataTable();
DataTable DT1 = new DataTable();
DT1.Columns.Add("USN");
DT1.Columns.Add("Name");
DT1.Columns.Add("FName");
DT2.Columns.Add("CC");
DT2.Columns.Add("C_Title");
DataRow DR = DT1.NewRow();
DR["USN"] = USN;
DR["Name"] = Name;
DR["FName"] = FName;
DT1.Rows.Add(DR);
DataRow DR1 = DT2.NewRow();
DR1["CC"] = CC;
DR1["C_Title"] = CTIT;
DT2.Rows.Add(DR1);
dataGridView1.DataSource = MergeColumns(DT1, DT2);


这篇关于合并数据表不按行方式工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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