PC Review


Reply
Thread Tools Rate Thread

Delete rows from DataTable

 
 
=?Utf-8?B?TmVvIFRoZSBPbmU=?=
Guest
Posts: n/a
 
      22nd Jul 2005
I need to delete some rows from a DataTable. I am not sure if the folllowing
code is supported:

void DeleteBlankRows(DataTable dt)
{
foreach(DataRow dr in dt.Rows)
{
if(AllFieldsEmpty(dr)) dr.Delete();
}
}

I am wondering if the deletion in middle of foreach will interferer with the
interal enumeration.
 
Reply With Quote
 
 
 
 
Miha Markic [MVP C#]
Guest
Posts: n/a
 
      22nd Jul 2005
Hi,

"Neo The One" <(E-Mail Removed)> wrote in message
news:89F734CF-0BAC-4BF9-BD87-(E-Mail Removed)...
>I need to delete some rows from a DataTable. I am not sure if the
>folllowing
> code is supported:
>
> void DeleteBlankRows(DataTable dt)
> {
> foreach(DataRow dr in dt.Rows)
> {
> if(AllFieldsEmpty(dr)) dr.Delete();
> }
> }
>
> I am wondering if the deletion in middle of foreach will interferer with
> the
> interal enumeration.


Yes, I wouldn't recommend that approach. The thing is that added rows are
actually removed from the list when Delete is invoked on them.
Instead, do a reverse loop (int i = dt.Rows.Count-1 ... i = 0)

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
SLODUG - Slovene Developer Users Group www.codezone-si.info


 
Reply With Quote
 
W.G. Ryan MVP
Guest
Posts: n/a
 
      22nd Jul 2005
Neo - as an FYI , keep in mind the distinction is w/ Remove vs. Delete.
Delete doesn't actually remove the rows from the collection ,it simply
changes their Rowstate - HOWEVER, the collection is 'modified' so it will
cause a problem.. Remove on the other hand, takes them from the collection
and as such, will also cause a problem with the foreach syntax. Now, if you
were to call .AccpetChanges in the middle of the foreach loop w/ Delete, it
will take the row out of the collection as well which will cause a problem.
I believe you can get through one pass w/ Delete, but it's definitely not
something you want to do.

So I guess that's a lot of babble I included just to confirm that it will
cause a problem ;-). Anyway, for this operation, I'd just use a for(int i)
loop.


"Neo The One" <(E-Mail Removed)> wrote in message
news:89F734CF-0BAC-4BF9-BD87-(E-Mail Removed)...
>I need to delete some rows from a DataTable. I am not sure if the
>folllowing
> code is supported:
>
> void DeleteBlankRows(DataTable dt)
> {
> foreach(DataRow dr in dt.Rows)
> {
> if(AllFieldsEmpty(dr)) dr.Delete();
> }
> }
>
> I am wondering if the deletion in middle of foreach will interferer with
> the
> interal enumeration.



 
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
Delete Rows in DataTable Sreppohcdoow Microsoft ADO .NET 0 26th Mar 2006 08:28 PM
DataTable DataRow Delete Duplicate Rows =?Utf-8?B?VmlqYXkgUG90ZQ==?= Microsoft C# .NET 4 9th Sep 2005 09:59 PM
Delete DataView rows from DataTable (should be simple) MattB Microsoft ADO .NET 1 21st Jun 2005 07:15 PM
delete all rows in a datatable jeff Microsoft VB .NET 5 17th Nov 2004 08:06 AM
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


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:10 PM.