473,326 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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
Jun 27 '08 #1
3 1491
for (int i = FoodDataTable.Rows.Count - 1; i >= 0; i--)

--
Pete
=========================================
I use Enterprise Core Objects (Domain driven design)
http://www.capableobjects.com/
=========================================
Jun 27 '08 #2
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
Jun 27 '08 #3
Thanks guys!

"Jeroen Mostert" <jm******@xs4all.nlwrote in message
news:48***********************@news.xs4all.nl...
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
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
21
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
1
by: zb | last post by:
I am writing a C# program that uses MySQL as database and MySQL ODBC 3.51 driver. It works fine all the way except this scenario that needs to be addressed. 1. Get a set of records to process...
9
by: 2DIL | last post by:
hello all experts, basically, i have a very small project, in which 97% of the work is complete. yet there is an issue which i cannot solve and i have no error to indicate what the problem...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.