473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Getting a subset of DataRows from a DataTable

I have a DataTable from which I only need a certain range of the DataRows.
What I would like to do is copy a range of rows from one DataTable to a new
DataTable like the following:

For i As Integer = start To last
table1.Rows.Add(table2.Rows(i))
Next

However, the code above gives an error saying the DataRow is already used in
another DataTable. Is there any easy way to copy a DataRow from one
DataTable to another? I tried to use the CopyTo method, but I was having
some trouble doing that. Can somebody help me here?
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 17 '07 #1
9 9290
Nathan,

As you use versions later then 2003 you can use very simple use the
Overloaded DataView.ToTable for that.

However you cannot create a table and then add rows. A row exist from items
which are described in the column collection from a table. In the datarow is
stored what table contains the collection, therefore your error message.

Cor

Nov 17 '07 #2
Hi Nathan,

Try this method:
table1.Rows.Add(table2.Rows[i].ItemArray);

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:OL**************@TK2MSFTNGP03.phx.gbl...
>I have a DataTable from which I only need a certain range of the DataRows.
What I would like to do is copy a range of rows from one DataTable to a new
DataTable like the following:

For i As Integer = start To last
table1.Rows.Add(table2.Rows(i))
Next

However, the code above gives an error saying the DataRow is already used
in another DataTable. Is there any easy way to copy a DataRow from one
DataTable to another? I tried to use the CopyTo method, but I was having
some trouble doing that. Can somebody help me here?
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 17 '07 #3
On 17 Nob, 17:53, "Miha Markic" <miha at rthand comwrote:
Hi Nathan,

Try this method:
table1.Rows.Add(table2.Rows[i].ItemArray);

--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & developmentwww.rthand.com
Blog:http://cs.rthand.com/blogs/blog_with_righthand/

"Nathan Sokalski" <njsokal...@hotmail.comwrote in message

news:OL**************@TK2MSFTNGP03.phx.gbl...
I have a DataTable from which I only need a certain range of the DataRows.
What I would like to do is copy a range of rows from one DataTable to a new
DataTable like the following:
For i As Integer = start To last
table1.Rows.Add(table2.Rows(i))
Next
However, the code above gives an error saying the DataRow is already used
in another DataTable. Is there any easy way to copy a DataRow from one
DataTable to another? I tried to use the CopyTo method, but I was having
some trouble doing that. Can somebody help me here?
--
Nathan Sokalski
njsokal...@hotmail.com
http://www.nathansokalski.com/
try this (untested):

dim table2 as new datatable

table2 = table1.clone()
for i = start to last
table2.importrow(table1.rows(i))
next

hope this helps
Nov 17 '07 #4
diego,

Is this not much easier?
dt2 = dt1.DefaultView.ToTable()

(The defaultview is the default dataview)

Cor

Nov 17 '07 #5
Is this not much easier?
dt2 = dt1.DefaultView.ToTable()

No because this wil copy all rows and the TS stated he wanted to have a
subset of the original rows
I just insert the itemarray from the source row in the destination table
this is afaik the shortest way and can be used in a table select or for each
loop without anny problems .

regards

michel
"Cor Ligthert[MVP]" <no************@planet.nlschreef in bericht
news:A9**********************************@microsof t.com...
diego,

Is this not much easier?
dt2 = dt1.DefaultView.ToTable()

(The defaultview is the default dataview)

Cor

Nov 17 '07 #6
Michel,

With the defaultview you can select the rows by the rowfilter and the
columns by the overloading part of the ToTable, can you do it with less
commands?

Cor

Nov 17 '07 #7
AFAIK

This wil not work so easy as you tell it Cor as the Totable and then
select method wil return a datarow array

so then you must loop through the datarow array , then another problem
occurs couse the select method returns not a strongly typed datarow so you
must typecast the object
or just asume the datarows are the same and use in your loop construct also
the standard datarow object ,so some more coding is required to get it
functional

and that against this
For i As Integer = start To last

table1.Rows.Add(table2.Rows(i).ItemArray)

Next

i guess is hart to beat in terms of perfomance , readability and used
resources

Or i might be missing something here , always ready to learn something new
so correct me if i am wrong

regards

Michel

"Cor Ligthert[MVP]" <no************@planet.nlschreef in bericht
news:21**********************************@microsof t.com...
Michel,

With the defaultview you can select the rows by the rowfilter and the
columns by the overloading part of the ToTable, can you do it with less
commands?

Cor

Nov 17 '07 #8
Michael,

The "DataTable.Select is returning an array of datarows.

The dataview.ToTable is returning a datatable.

You saw my sample, I seldom do this for this kind of messages, but I made it
in the designer, that is impossible if it cannot return a table.

However because it is you I extend it a little bit.

Private Sub Form1_Load(ByVal sender As System .Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

'To create a sample table
Dim dt1 As New DataTable
Dim dtc1 As New DataColumn("Michel")
Dim dtc2 As New DataColumn("Cor")
dt1.Columns.Add(dtc1)
dt1.Columns.Add(dtc2)
For i = 1 To 10
Dim dr As DataRow = dt1.NewRow
dr(0) = i.ToString & i.ToString
dr(1) = i
dt1.Rows.Add(dr)
Next
'End of building Datatable

'Therefore all you need is
dt1.DefaultView.RowFilter = "Michel 33 And Michel < 66"
Dim dt2 As DataTable = dt1.DefaultView.ToTable(False, "Cor")
'To Show in a Grid
DataGridView1.DataSource = dt2
End Sub

The True would make a distinct table, I used 2008 beta, this overload is in
my mind not in VS2005 I thought that you need in that he new tablename as
first parameter. However you can do it for the rest exactly like this.

:-)

Cor

Nov 17 '07 #9
for i as integer = start to last
table1.ImportRow(table2.Rows(i))
Next

"Nathan Sokalski" <nj********@hotmail.comwrote in message
news:OL**************@TK2MSFTNGP03.phx.gbl...
>I have a DataTable from which I only need a certain range of the DataRows.
What I would like to do is copy a range of rows from one DataTable to a new
DataTable like the following:

For i As Integer = start To last
table1.Rows.Add(table2.Rows(i))
Next

However, the code above gives an error saying the DataRow is already used
in another DataTable. Is there any easy way to copy a DataRow from one
DataTable to another? I tried to use the CopyTo method, but I was having
some trouble doing that. Can somebody help me here?
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Nov 17 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Prince | last post by:
hi all, I need to insert a datarows collection from a datatable into a datagrid?can anyone tell me how to do??? Thanx in advance..
1
by: psb | last post by:
I thought this was weird?? is this a bug in framework 1.0??? (1.0 is the version I am running against) --------------------------- dim dtAll as new datatable dim dtTmp as datatable dtTmp =...
5
by: Nathan Sokalski | last post by:
I am writing an ASP.NET application in which I need to copy DataRows from one DataTable to another. When I use code such as the following: temprows = nodes.Select("state='PA'")...
9
by: Brad | last post by:
I have written some code to manipulate data/records in a MASTER (order header) and DETAIL (order details) tables. What I have written is too extensive to post but essentially trying to: 1....
3
by: creator_bob | last post by:
How do I create an array of datarows from a sorted list? I put a bunch of datarows into a sorted list to sort them. Then I got an array of the sorted elements. However, I cannot typecast them. ...
3
by: H | last post by:
I have a dataset with Customers, Orders, and Items tables. It has datarelations set between Customers-Orders and Orders-Items It has foreignkeyconstraints with update/delete rules set to cascade...
3
by: zhshqzyc | last post by:
remove other DataRows from your DataTable Hi, I want to display the top three rows of the table. So I just want to remove the other rows. Suppose the datas are not from a database. How to do...
8
by: jehugaleahsa | last post by:
Hello: We wrote an entire application where we add our DataRows to our DataTables immediately. However, we have to shut off our constraints to do this. We would like to use detached DataRows to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.