Groups | Blog | Home
all groups > dotnet ado.net > july 2007 >

dotnet ado.net : Merge DataRow Items?


lucius
7/25/2007 3:52:51 PM

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.
Benny Skjold Tordrup
7/26/2007 12:00:00 AM
Do you have a primary key column defined in the DataTable?


"lucius" <lucius@newsgroup.nospam> skrev i en meddelelse
news:qeafa3tj2aqtm5l42mkgp8he6g1n3m9omi@4ax.com...
[quoted text, click to view]

v-wywang NO[at]SPAM online.microsoft.com
7/26/2007 12:00:00 AM
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.
v-wywang NO[at]SPAM online.microsoft.com
7/30/2007 11:10:22 AM
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.
AddThis Social Bookmark Button