473,466 Members | 1,374 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 20 '05 #1
5 2454
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 20 '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 20 '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 21 '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 21 '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: 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 =...
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....
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'")...
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. ...
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,...
9
by: Nathan Sokalski | last post by:
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...
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...
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.