PC Review


Reply
Thread Tools Rate Thread

Copying DataRows to another DataTable

 
 
Nathan Sokalski
Guest
Posts: n/a
 
      19th Nov 2005
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.
--
Nathan Sokalski
(E-Mail Removed)
http://www.nathansokalski.com/


 
Reply With Quote
 
 
 
 
Scott M.
Guest
Posts: n/a
 
      19th Nov 2005
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.
> --
> Nathan Sokalski
> (E-Mail Removed)
> http://www.nathansokalski.com/
>



 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      19th Nov 2005
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


 
Reply With Quote
 
=?Utf-8?B?UGF1bE5hdWRl?=
Guest
Posts: n/a
 
      21st Nov 2005
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.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.
> > --
> > Nathan Sokalski
> > (E-Mail Removed)
> > http://www.nathansokalski.com/
> >

>
>
>

 
Reply With Quote
 
Scott M.
Guest
Posts: n/a
 
      21st Nov 2005
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" <(E-Mail Removed)> wrote in message
news:0F1F3262-7264-468C-A1B6-(E-Mail Removed)...
> 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.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.
>> > --
>> > Nathan Sokalski
>> > (E-Mail Removed)
>> > http://www.nathansokalski.com/
>> >

>>
>>
>>



 
Reply With Quote
 
=?Utf-8?B?UGF1bE5hdWRl?=
Guest
Posts: n/a
 
      22nd Nov 2005
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" <(E-Mail Removed)> wrote in message
> news:0F1F3262-7264-468C-A1B6-(E-Mail Removed)...
> > 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.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.
> >> > --
> >> > Nathan Sokalski
> >> > (E-Mail Removed)
> >> > http://www.nathansokalski.com/
> >> >
> >>
> >>
> >>

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying DataRows to another DataTable Nathan Sokalski Microsoft ASP .NET 5 22nd Nov 2005 07:09 AM
Copying DataRows to another DataTable Nathan Sokalski Microsoft VB .NET 5 22nd Nov 2005 07:09 AM
Retrieving datarows using DataTable.Select method and adding it to new DataTable C.Anand via DotNetMonster.com Microsoft ADO .NET 4 4th Apr 2005 05:21 PM
DataRows into a DataTable =?Utf-8?B?TUVSNzg=?= Microsoft ADO .NET 5 6th Jan 2005 02:47 PM
DataTable DataRows =?Utf-8?B?RGVubmlz?= Microsoft VB .NET 0 6th Jan 2005 01:23 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:53 AM.