Copying DataRows to another DataTable


N

Nathan Sokalski

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.vb: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.
 
Ad

Advertisements

S

Scott M.

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
 
G

Guest

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. said:
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.vb: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.
 
S

Scott M.

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 said:
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. said:
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.vb: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.
 
Ad

Advertisements

G

Guest

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. said:
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 said:
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. said:
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.vb: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.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top