Merge DataRow Items?

L

lucius

I have a DataTable with one row and I did a Merge() from another
DataTable and now I have two rows. How can I get the rows to be a
single row (assuming row 0 is the "dominant" one).

Thanks.
 
W

WenYuan Wang [MSFT]

Hello Lucius,
Thanks for Benny's response.

DataTable.Merge() method merge two tables by Primary key.

In your case, the table that has to be merged needs the same primary key as
the destination table.
For example:

System.Data.DataTable dt1 = new System.Data.DataTable();
dt1.Columns.Add("c1");
dt1.Columns.Add("c2");
dt1.PrimaryKey = new System.Data.DataColumn[] { dt1.Columns[0]};
dt1.Rows.Add(new object[] { 1, 1 });
dt1.Rows.Add(new object[] { 2, 2 });
dt1.Rows.Add(new object[] { 3, 3 });

System.Data.DataTable dt2 = new System.Data.DataTable();
dt2.Columns.Add("c1");
dt2.Columns.Add("c2");
dt2.PrimaryKey = new System.Data.DataColumn[] { dt2.Columns[0]
};
dt2.Rows.Add(new object[] { 1, 4 });
dt2.Rows.Add(new object[] { 2, 5 });
dt2.Rows.Add(new object[] { 3, 6 });

dt1.Merge(dt2);

Result: dt1
C1, C2
1,4
2,5
3,6

Hope this helps. If there is anything unclear, please feel free to update
here. I'm glad to assit you.
Have a great day,
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

WenYuan Wang [MSFT]

Hello Lucius,

This is Wen Yuan again. I haven't heard from you about a couple of days.
I just want to check if there is anything we can help with.
Have you resolved the issue so far?
Please feel free to update here if you have any concern. We are glad to
assist you.

Have a great day,
Best regards,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top