PC Review


Reply
Thread Tools Rate Thread

How delete rows from DataTable and avoid "no row at position" error?

 
 
Ronald S. Cook
Guest
Posts: n/a
 
      19th Jun 2008
Consider the following DataTable:

FoodId FoodName FoodType
1 Apple Fruit
2 Pear Fruit
3 Corn Vegetable
4 Bread Starch
5 Cereal Starch
6 Carrot Vegetable
7 Grapes Fruit

I want to delete all records where FoodType = Starch.

If I do the below, however, the rowcount will decrement with every delete
and I end up with a "No row at position... " error.

for (int i=0; i <= FoodDataTable.Rows.Count- 1; i++)
{
if (FoodDataTable.Rows(i)("FoodType").ToString() == "Starch"
FoodDataTable.Rows(i).Delete();
}

Can anyone help me around this?

Thanks for any help,
Ron


 
Reply With Quote
 
 
 
 
Peter Morris
Guest
Posts: n/a
 
      19th Jun 2008
for (int i = FoodDataTable.Rows.Count - 1; i >= 0; i--)



--
Pete
=========================================
I use Enterprise Core Objects (Domain driven design)
http://www.capableobjects.com/
=========================================


 
Reply With Quote
 
Jeroen Mostert
Guest
Posts: n/a
 
      19th Jun 2008
Ronald S. Cook wrote:
> Consider the following DataTable:
>
> FoodId FoodName FoodType
> 1 Apple Fruit
> 2 Pear Fruit
> 3 Corn Vegetable
> 4 Bread Starch
> 5 Cereal Starch
> 6 Carrot Vegetable
> 7 Grapes Fruit
>
> I want to delete all records where FoodType = Starch.
>
> If I do the below, however, the rowcount will decrement with every
> delete and I end up with a "No row at position... " error.
>
> for (int i=0; i <= FoodDataTable.Rows.Count- 1; i++)
> {
> if (FoodDataTable.Rows(i)("FoodType").ToString() == "Starch"
> FoodDataTable.Rows(i).Delete();
> }
>

foreach (DataRow row in FoodDataTable.Select("FoodType = 'Starch'")) {
row.Delete();
}

--
J.
http://symbolsprose.blogspot.com
 
Reply With Quote
 
Ronald S. Cook
Guest
Posts: n/a
 
      19th Jun 2008
Thanks guys!

"Jeroen Mostert" <(E-Mail Removed)> wrote in message
news:485a92b8$0$14358$(E-Mail Removed)...
> Ronald S. Cook wrote:
>> Consider the following DataTable:
>>
>> FoodId FoodName FoodType
>> 1 Apple Fruit
>> 2 Pear Fruit
>> 3 Corn Vegetable
>> 4 Bread Starch
>> 5 Cereal Starch
>> 6 Carrot Vegetable
>> 7 Grapes Fruit
>>
>> I want to delete all records where FoodType = Starch.
>>
>> If I do the below, however, the rowcount will decrement with every delete
>> and I end up with a "No row at position... " error.
>>
>> for (int i=0; i <= FoodDataTable.Rows.Count- 1; i++)
>> {
>> if (FoodDataTable.Rows(i)("FoodType").ToString() == "Starch"
>> FoodDataTable.Rows(i).Delete();
>> }
>>

> foreach (DataRow row in FoodDataTable.Select("FoodType = 'Starch'")) {
> row.Delete();
> }
>
> --
> J.
> http://symbolsprose.blogspot.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
"Right" way to delete a datatable record using auto-generated ADO.Netobjects in C# 2005 Dathan Microsoft ADO .NET 1 8th Jan 2009 06:03 AM
Delete rows created when Error 3159 "Not a valid bookmark" encount =?Utf-8?B?SXJpbmUgRi4=?= Microsoft Access Forms 3 21st Jul 2005 07:46 AM
"Avoid Inserting Rows / Columns" Gokul77Jay Microsoft Excel Misc 3 9th Apr 2004 03:00 PM
NoNullAllowedException at DataTable.Rows.Add(Me.mvarRow) (Re: "Assembly of Ressource not found") Microsoft Dot NET Compact Framework 0 13th Feb 2004 12:02 PM
Maintaining Record Position in DataGrid/DataTable After "Refresh" CSerpent Microsoft VB .NET 0 30th Sep 2003 05:37 PM


Features
 

Advertising
 

Newsgroups
 


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