How can I delete a Item in foreach loop

Discussion in 'ASP .Net' started by ad, Jun 5, 2005.

  1. ad

    ad Guest

    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();

    }
    }
    ----------------------------------------------------------------------------
    ----------------------------------
     
    ad, Jun 5, 2005
    #1
    1. Advertising

  2. ad

    AlexS Guest

    You can't.

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

    HTH
    Alex
    "ad" <> wrote in message
    news:OfNW%...
    > 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();
    >
    > }
    > }
    > --------------------------------------------------------------------------

    --
    > ----------------------------------
    >
    >
     
    AlexS, Jun 5, 2005
    #2
    1. Advertising

  3. 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" <> wrote in message
    news:OfNW#...
    > 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();
    >
    > }
    > }
    > --------------------------------------------------------------------------

    --
    > ----------------------------------
    >
    >
     
    Peter Rilling, Jun 6, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mike P

    Foreach item is Outlook inbox

    Mike P, May 29, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    375
    Mark Rae
    May 29, 2007
  2. Ratko
    Replies:
    7
    Views:
    294
    Reedick, Andrew
    Jul 17, 2008
  3. Fencer
    Replies:
    6
    Views:
    357
    Arved Sandstrom
    Dec 2, 2009
  4. Namor
    Replies:
    0
    Views:
    301
    Namor
    Jan 19, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    397
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page