473,320 Members | 1,848 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,320 software developers and data experts.

Copying DataRows to another DataTable

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'")
temptable.Clear()
For Each row As DataRow In temprows
temptable.Rows.Add(row)
Next
I recieve the following error:

[ArgumentException: This row already belongs to another table.]
System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32 pos)
+450
System.Data.DataRowCollection.Add(DataRow row) +14
WebApplication1.WebForm2.Page_Load(Object sender, EventArgs e) in
C:\Inetpub\wwwroot\WebApplication1\WebForm2.aspx.v b:43
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +739
I could not find any method that is used for copying a DataRow (like the
DataTable class has a Copy() method). Is there any way to copy individual
DataRows without manually reading each property and creating a new DataRow?
Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 23 '05 #1
5 14296
temprows = nodes.Select("state='PA'")
temptable.Clear()
For Each row As DataRow In temprows
Dim newRow As New DataRow = row
temptable.Rows.Add(row)
Next




I recieve the following error:

[ArgumentException: This row already belongs to another table.]
System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32
pos) +450
System.Data.DataRowCollection.Add(DataRow row) +14
WebApplication1.WebForm2.Page_Load(Object sender, EventArgs e) in
C:\Inetpub\wwwroot\WebApplication1\WebForm2.aspx.v b:43
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +739
I could not find any method that is used for copying a DataRow (like the
DataTable class has a Copy() method). Is there any way to copy individual
DataRows without manually reading each property and creating a new
DataRow? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

Nov 23 '05 #2
Nathan,

The methode for that is datatable.importrow

http://msdn.microsoft.com/library/de...rtrowtopic.asp

(A datarow holds in its properties the table that is used to create it. In
that are the columndescription. Therefore it can not reference to more than
oner datatable.)

I hope this helps,

Cor
Nov 23 '05 #3
Is it possible to use this method to copy and paste rows from and to the same
datagrid and if so, how do one get the selected rows for the copy action?

The user simply need to duplicate some records in a datagrid (source =
filtered dataview) and paste it over other existing records. (the user will
then edit some of these values). Typically, only the selected cells need to
be copied since the primary key columns need to be left unchanged.

"Scott M." wrote:
temprows = nodes.Select("state='PA'")
temptable.Clear()
For Each row As DataRow In temprows
Dim newRow As New DataRow = row
temptable.Rows.Add(row)
Next




I recieve the following error:

[ArgumentException: This row already belongs to another table.]
System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32
pos) +450
System.Data.DataRowCollection.Add(DataRow row) +14
WebApplication1.WebForm2.Page_Load(Object sender, EventArgs e) in
C:\Inetpub\wwwroot\WebApplication1\WebForm2.aspx.v b:43
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +739
I could not find any method that is used for copying a DataRow (like the
DataTable class has a Copy() method). Is there any way to copy individual
DataRows without manually reading each property and creating a new
DataRow? Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/


Nov 23 '05 #4
If you are asking if you can copy a row displayed in a datagrid and then
paste that row back into the datagrids source so that it will overwright the
first row, then my response is, you are going about this incorrectly. What
it seems that you are describing is basically just editing value from a row
in a datagrid. In this case, you need to use the PK of the row being
displayed in the grid to locate the same underlying row of data in the grids
datasource (datatable). Once you've done that, you can just update that
rows values and ultimately, the original datasource.
"PaulNaude" <Pa*******@discussions.microsoft.com> wrote in message
news:0F**********************************@microsof t.com...
Is it possible to use this method to copy and paste rows from and to the
same
datagrid and if so, how do one get the selected rows for the copy action?

The user simply need to duplicate some records in a datagrid (source =
filtered dataview) and paste it over other existing records. (the user
will
then edit some of these values). Typically, only the selected cells need
to
be copied since the primary key columns need to be left unchanged.

"Scott M." wrote:
temprows = nodes.Select("state='PA'")
temptable.Clear()
For Each row As DataRow In temprows
Dim newRow As New DataRow = row
temptable.Rows.Add(row)
Next


>
>
> I recieve the following error:
>
> [ArgumentException: This row already belongs to another table.]
> System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32
> pos) +450
> System.Data.DataRowCollection.Add(DataRow row) +14
> WebApplication1.WebForm2.Page_Load(Object sender, EventArgs e) in
> C:\Inetpub\wwwroot\WebApplication1\WebForm2.aspx.v b:43
> System.Web.UI.Control.OnLoad(EventArgs e) +67
> System.Web.UI.Control.LoadRecursive() +35
> System.Web.UI.Page.ProcessRequestMain() +739
>
>
> I could not find any method that is used for copying a DataRow (like
> the
> DataTable class has a Copy() method). Is there any way to copy
> individual
> DataRows without manually reading each property and creating a new
> DataRow? Thanks.
> --
> Nathan Sokalski
> nj********@hotmail.com
> http://www.nathansokalski.com/
>


Nov 23 '05 #5
The problem is that I don't know how to provide the user with copy and paste
functionality in a datagrid. In my specific case I hide the primary key
columns and by selecting values in other datagrids, the grid in question is
filtered. The user need to enter values for all possible combinations of
selected filter values, but may find it usefull to copy and paste ranges of
cells which are identical or simmilar (which will radically reduce the number
of cell by cell entering of values).

Is this an option, or is the programming too advanced?

"Scott M." wrote:
If you are asking if you can copy a row displayed in a datagrid and then
paste that row back into the datagrids source so that it will overwright the
first row, then my response is, you are going about this incorrectly. What
it seems that you are describing is basically just editing value from a row
in a datagrid. In this case, you need to use the PK of the row being
displayed in the grid to locate the same underlying row of data in the grids
datasource (datatable). Once you've done that, you can just update that
rows values and ultimately, the original datasource.
"PaulNaude" <Pa*******@discussions.microsoft.com> wrote in message
news:0F**********************************@microsof t.com...
Is it possible to use this method to copy and paste rows from and to the
same
datagrid and if so, how do one get the selected rows for the copy action?

The user simply need to duplicate some records in a datagrid (source =
filtered dataview) and paste it over other existing records. (the user
will
then edit some of these values). Typically, only the selected cells need
to
be copied since the primary key columns need to be left unchanged.

"Scott M." wrote:
temprows = nodes.Select("state='PA'")
temptable.Clear()
For Each row As DataRow In temprows
Dim newRow As New DataRow = row
temptable.Rows.Add(row)
Next

>
>
> I recieve the following error:
>
> [ArgumentException: This row already belongs to another table.]
> System.Data.DataTable.InsertRow(DataRow row, Int32 proposedID, Int32
> pos) +450
> System.Data.DataRowCollection.Add(DataRow row) +14
> WebApplication1.WebForm2.Page_Load(Object sender, EventArgs e) in
> C:\Inetpub\wwwroot\WebApplication1\WebForm2.aspx.v b:43
> System.Web.UI.Control.OnLoad(EventArgs e) +67
> System.Web.UI.Control.LoadRecursive() +35
> System.Web.UI.Page.ProcessRequestMain() +739
>
>
> I could not find any method that is used for copying a DataRow (like
> the
> DataTable class has a Copy() method). Is there any way to copy
> individual
> DataRows without manually reading each property and creating a new
> DataRow? Thanks.
> --
> Nathan Sokalski
> nj********@hotmail.com
> http://www.nathansokalski.com/
>


Nov 23 '05 #6

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

Similar topics

1
by: Job Lot | last post by:
My DataGrid is bound to a datatable named “dtClient” with following columns Date, BalanceCF, Income, Expenses, Withdrawls and BalanceCF. BalanceCF is an expression column. The column Withdrawals...
3
by: VM | last post by:
How can I copy the contents of one datatable to another datatable? I've already the source table so I only need to copy its rows. Thanks.
1
by: VMI | last post by:
I have a filled datatable and I need to copy this data into an Access table with exactly the same table structure. Can I do this without having to run an Insert query for every line of the...
1
by: J. Babe | last post by:
I was wondering how to add the first 20 rows from one datatable to another datatable without getting the error: This row already belongs to another table
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'")...
0
by: PC | last post by:
I have a dataset with in it a single datatable (say Table1) this datatable is the result of a union query on several tables i would like to create a second datatable (say Table2) in that dataset...
0
by: sravan_reddy001 | last post by:
SqlConnection sql; SqlDataAdapter sqlda; DataSet sqlds, msads; OleDbConnection msa; OleDbDataAdapter msada; //DataRow drs,drm; DataTable ab, abcopy; private void Form1_Load(object sender,...
7
by: NeverLift | last post by:
This is probably answered elsewhere, but I've searched the Web and VBA for Excel manual, find no answers. I have a VBA-coded macro in an Excel workbook that is to open another existing workbook --...
1
by: tshad | last post by:
Running on VS.net 2005, I am trying to copy rows from my datatable to another datatable in the same dataset. The schema would be identical. I need to make the table name "forms" as I am...
11
by: John Dann | last post by:
Is there a concise/efficient way to retrieve blocks of rows from a datatable with VB2005? I've got a datatable (let's call it AllData), constructed programmatically, that contains a lot of...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shllpp 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.