Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to merge datatables. But I am not getting the desired result.
C#
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

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 :
HTML
USN        |Name     |FName        |CC       |CTitle
4PS12MCA28  Partha    Krishnappa    P11MCA41  JAVA

Please help. Thank you
EDIT :
The o/p which I am getting is :
HTML
USN        |Name  |FName     |CC       |CTitle
4PS12MCA28 Partha Krishnappa
                              P11MCA41  JAVA

I dono exactly where I am going wrong. Thank you.
Posted
Updated 5-Jun-15 23:46pm
v2
Comments
OriginalGriff 6-Jun-15 5:18am    
Don't try to show us an image - particularly when it doesn't work - write it out and show us!
It can't be that much work for you can it? Especially considering you want us to do something to help you?
partha143 6-Jun-15 5:28am    
I am sorry Sir. I thought attaching an image would give you better understanding about my problem. The o/p which I am getting is :
<pre lang="HTML">
USN |Name |FName |CC |CTitle
4PS12MCA28 Partha Krishnappa
P11MCA41 JAVA
</pre>
I dono exactly where I am going wrong. Thank you.
OriginalGriff 6-Jun-15 5:33am    
Um...I see no difference between the output you get and the output you expect!
What am I missing?

Oh, and use the "Improve question" widget to move that into your question so that it shows formatted.
partha143 6-Jun-15 5:50am    
Edited in main question. Have a look at EDIT part. My main question was this :
http://www.codeproject.com/Questions/998289/Adding-columns-to-DataSet?arn=0. As I dint received any suggestions on that, I figured out the solution as to use 2 data tables. I kindly request you to have a look at my main question and suggest me if my current solution is good/bad. Thank you.

1 solution

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:
C#
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:
C#
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);
 
Share this answer
 
Comments
partha143 6-Jun-15 6:28am    
Thank you sir. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900