PC Review


Reply
Thread Tools Rate Thread

Copying Rows to a Datatable

 
 
byrd48
Guest
Posts: n/a
 
      2nd Feb 2007
Hi,
am attempting to create a DataTable by copying a table from a data
set, then filter the rows as follows:

DataTable dt = DataSet1.Tables[0].Copy;

DataRow[] dr = dt.Select("myexpression");

dt.Rows.Clear();

dt.Rows.Add(dr);

I have validated that teh number of columns in the datatable matches
the number of items in the DataRow.ItemArray, but it still throws an
exception on the last line that the index of dr is greater than the
number of columns in the datatable.
Thanks in advance,

Jon

 
Reply With Quote
 
 
 
 
Alvin Bruney [MVP]
Guest
Posts: n/a
 
      4th Feb 2007
I think you are a bit confused, at least the code is confused. What you have
in code clones a datatable and filters it to a subset of rows. Then, source
dataset is cleared which also clears the target dataset because the
dt.select statement actually is a shallow copy/reference assignment and not
a true deep copy. Then you turn around and add the empty datarow objects
back into the cleared datatable. I'm certain that this is not what you want
to do. Otherwise, i'm confused.

--
Regards,
Alvin Bruney
------------------------------------------------------
Shameless author plug
Excel Services for .NET is coming...
OWC Black book on Amazon and
www.lulu.com/owc


"byrd48" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi,
> am attempting to create a DataTable by copying a table from a data
> set, then filter the rows as follows:
>
> DataTable dt = DataSet1.Tables[0].Copy;
>
> DataRow[] dr = dt.Select("myexpression");
>
> dt.Rows.Clear();
>
> dt.Rows.Add(dr);
>
> I have validated that teh number of columns in the datatable matches
> the number of items in the DataRow.ItemArray, but it still throws an
> exception on the last line that the index of dr is greater than the
> number of columns in the datatable.
> Thanks in advance,
>
> Jon
>



 
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 records from datatable to datatable in dataset tshad Microsoft C# .NET 1 24th Jun 2008 01:39 AM
DataTable.Select() - is it possible to pass rows back to datatable =?Utf-8?B?QW5kcmUgUmFuaWVyaQ==?= Microsoft ADO .NET 5 9th Nov 2005 05:10 PM
How can I use real SQL on a DataTable? i.e. not array of rows using a filter... as in DataTable.Select Dan V. Microsoft C# .NET 3 1st Jul 2004 03:06 PM
copying rows from one datatable to another Microsoft C# .NET 1 5th Apr 2004 07:41 PM
Re: copying data rows from one datatable to another Nicholas Paldino [.NET/C# MVP] Microsoft C# .NET 0 30th Mar 2004 05:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:32 AM.