|
-
Oct 11th, 2007, 01:29 PM
#1
Thread Starter
New Member
[RESOLVED] How to merge rows of a datatable to another datatable
Hi all,
I'm searching the quickest way to add the rows of a datatable to another datatable.
I can't use merge because i need to filter the rows of the first datatable in order to take only certain rows.
Does anybody knows what0s the quickest way to perform this operation?
Thanks in advance
Last edited by jackmoros; Oct 12th, 2007 at 07:32 AM.
-
Oct 11th, 2007, 02:16 PM
#2
Re: How to merge rows of a datatable to another datatable
The first thing that come to my mind, is to loop throught the First DataTable, then using ImportRow, your may append them to a second DataTable.
Code:
Dim dt1 As New DataTable
Dim dt2 As New DataTable
For Each dr As DataRow In dt1.Rows
dt1.Rows.Remove(dr)
dt2.ImportRow(dr)
Next
-
Oct 11th, 2007, 02:52 PM
#3
Addicted Member
Re: How to merge rows of a datatable to another datatable
I guess you could use a DataView to filter out what you need, and then copy those filtered rows over to your second DataTable?
-
Oct 11th, 2007, 07:47 PM
#4
Re: How to merge rows of a datatable to another datatable
vb.net Code:
For Each row As DataRow In table1.Select("filter goes here")
table2.ImportRow(row)
Next row
-
Oct 12th, 2007, 07:31 AM
#5
Thread Starter
New Member
Re: How to merge rows of a datatable to another datatable
Thanks to all of you, eventually i used the first method, the one proposed by Zakary, it's fast and seems to work properly.
P.S. I tried to use the method proposet by jmcilhinney but it didn't work, the error is that the row already belongs to another datatable.
Thanks a lot
-
Oct 12th, 2007, 10:08 AM
#6
Re: [RESOLVED] How to merge rows of a datatable to another datatable
Firstly, Zakary's method doesn't work for your request because it doesn't provide a filter. Secondly, my code will work as is. The only reason you'd get that error message is if you used Rows.Add instead of ImportRow.
Last edited by jmcilhinney; Oct 12th, 2007 at 10:15 AM.
-
Oct 12th, 2007, 10:59 AM
#7
Thread Starter
New Member
Re: [RESOLVED] How to merge rows of a datatable to another datatable
Ops sorry infact i was using Zakary's but checking if the row was good row by row...
-
Oct 12th, 2007, 12:28 PM
#8
Re: [RESOLVED] How to merge rows of a datatable to another datatable
Both method are good, in my method, I was just considering that your filter was already done.
-
Oct 12th, 2007, 09:56 PM
#9
Re: [RESOLVED] How to merge rows of a datatable to another datatable
Both method are good, in my method, I was just considering that your filter was already done.
But how can you assume that if you're taking the rows directly from a DataTable? It's not possible to filter an actual DataTable. You have to either use a DataView or the table's Select method to filter.
Ops sorry infact i was using Zakary's but checking if the row was good row by row...
Why do that when the table can do it for you? How is my code any different to Zakary's except that it does the filtering for you? If his works then mine works, but it means you don't have to do any manual checking. Like I said, if you got the error message you said you did then you did NOT use my code. You must have been calling Rows.Add rather than ImportRow.
-
Oct 13th, 2007, 06:08 AM
#10
Thread Starter
New Member
Re: [RESOLVED] How to merge rows of a datatable to another datatable
Ok, so next week i'll try again to use your method, but this time i'll pay more attention.
Txs again
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|