473,396 Members | 1,804 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,396 software developers and data experts.

How can I delete a Item in foreach loop

ad
I want to delete a DataRowView in a DataView if the DataRowView not checked
OK.
(CheckRow is a function for checking )
I used the codes below:
But when some row is delete, it fail , the error message is

Collection was modified; enumeration operation may not execute.

How can I do that?

----------------------------------------------------------------------------
----------------------------------

foreach (DataRowView drv in myDataView)
{
if ( !CheckRow(drv) )
{
drv.Delete();

}
}
----------------------------------------------------------------------------
----------------------------------
Nov 19 '05 #1
2 1600
You can't.

Collect rows to delete inside foreach loop, remove them outside.

HTH
Alex
"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:Of***************@TK2MSFTNGP15.phx.gbl...
I want to delete a DataRowView in a DataView if the DataRowView not checked OK.
(CheckRow is a function for checking )
I used the codes below:
But when some row is delete, it fail , the error message is

Collection was modified; enumeration operation may not execute.

How can I do that?

-------------------------------------------------------------------------- -- ----------------------------------

foreach (DataRowView drv in myDataView)
{
if ( !CheckRow(drv) )
{
drv.Delete();

}
}
-------------------------------------------------------------------------- -- ----------------------------------

Nov 19 '05 #2
For things like this, I usually loop backwards and remove the items when
found, for instance

for( int i=drv.Count; i!=0; i-- ){
// remove item here.
}

The reason for going backwards is that it preserves the index position so
you never need to worry about whether you need skip an index or go back one
or something else.

"ad" <ad@wfes.tcc.edu.tw> wrote in message
news:Of*************@TK2MSFTNGP15.phx.gbl...
I want to delete a DataRowView in a DataView if the DataRowView not checked OK.
(CheckRow is a function for checking )
I used the codes below:
But when some row is delete, it fail , the error message is

Collection was modified; enumeration operation may not execute.

How can I do that?

-------------------------------------------------------------------------- -- ----------------------------------

foreach (DataRowView drv in myDataView)
{
if ( !CheckRow(drv) )
{
drv.Delete();

}
}
-------------------------------------------------------------------------- -- ----------------------------------

Nov 19 '05 #3

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

Similar topics

5
by: Nico | last post by:
Hello folks, I am currently storing a set of objects inside an array, $itemlist = array(); $itemlist = new item("myitem"); //... and I am looking to develop a search function, which...
2
by: Michal Januszczyk | last post by:
Hello. Sometimes I encounter the following problem: foreach( element of some collection) { delete this element; } of course, the runtime would not allow to modify
4
by: baylor | last post by:
i have a DataSet (TDS). It has 4 rows in it. i want to erase those. For the life of me i can't figure out how Option 1: foreach foreach (DataRow row in dataSet.MY_TABLE.Rows) { row.Delete(); }...
2
by: James | last post by:
How would I skip an item in a foreach loop without breaking out of the loop altogether? foreach(SearchResult resCol1 in resCol) { string myString = resCol1.Properties.ToString(); if...
3
by: mkiger | last post by:
Hey Guys. I'm making an asp.net app and I have a question regarding listboxes. I have a list box and I would like some of the items to be bold based on certian criteria. Is this possible? matt
2
by: Sparky Arbuckle | last post by:
I've created 2 listboxes with buttons that transfer items between the two. The idea is that one of the listboxes will have a collection of items to be deleted from the database. I haven't found any...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
2
by: John Kelsey | last post by:
I am an old, longtime C programmer surprised and confused by an error message I'm getting from my VS2005 compiler... "Cannot pass 'Item' as a ref or out argument because it is a 'foreach...
1
by: jmarcrum | last post by:
Hello all! i have a “monitor-type” program, where my program recognizes my defined "commands”, and executes the given command, or produces an error message that says “unrecognized command”. After...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.