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.
 
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.
 
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.
 
Joined
May 21, 2009
Messages
1
Reaction score
0
Copying DataRows

DataTable customerTable = new DataTable( "Customers" );
// Add columns
customerTable.Columns.Add( "id", typeof(int) );
customerTable.Columns.Add( "name", typeof(string) );

// Set PrimaryKey
customerTable.Columns[ "id" ].Unique = true;
customerTable.PrimaryKey = new DataColumn[] { customerTable.Columns["id"] };

// Add ten rows
for( int id=1; id<=10; id++ )
{
customerTable.Rows.Add(
new object[] { id, string.Format("customer{0}", id) } );
}
customerTable.AcceptChanges();

// Add another ten rows
for( int id=11; id<=20; id++ )
{
customerTable.Rows.Add(
new object[] { id, string.Format("customer{0}", id) } );
}

string strExpr;
string strSort;

strExpr = "id > 5";
// Sort descending by column named CompanyName.
strSort = "name asc,id asc";
// Use the Select method to find all rows matching the filter.
DataRow[] foundRows = customerTable.Select( strExpr, strSort);

DataTable dt = new DataTable( "Customers" );
// Add columns
dt.Columns.Add( "id", typeof(int) );
dt.Columns.Add( "name", typeof(string) );

// Set PrimaryKey
dt.Columns[ "id" ].Unique = true;
dt.PrimaryKey = new DataColumn[] { dt.Columns["id"] };

foreach (DataRow dRow in foundRows)
{
DataRow dr = dt.NewRow();
dr["id"] = dRow["id"];
dr["name"] = dRow["name"];
dt.Rows.Add(dr);
}
dataGrid1.DataSource = dt ;
dataGrid1.Refresh();
 

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

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top