delete rows on condition

7 posts, 1 answers
  1. Filleau
    Filleau avatar
    114 posts
    Member since:
    Jun 2006

    Posted 29 Apr 2011 Link to this post

    Hi

    I would like to delete some rows on condition.
    My Grid don't use database and is filled by code

    So I want to delete all row where column named "ColA" = "b"

    I work with VB...
  2. Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 29 Apr 2011 Link to this post

    Hello Filleau,

    Please take a look at the following help article.

    Just iterate the Rows.ToArray() and remove the rows that match a specific condition.

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Filleau
    Filleau avatar
    114 posts
    Member since:
    Jun 2006

    Posted 29 Apr 2011 Link to this post

    That don't help me... this is for a row

    Actually I tryed like this

     

     

    For Each row1 As GridViewRowInfo In RadGridView1.Rows

     

     

     

     

        If row1.Cells("Statut").Value <> "b" Then row1.Delete()

     

     

     

     

    Next

     



    And of course I got an error because I change the collection

    I was wondering if there is a way to do this like a SQL command
  5. Answer
    Emanuel Varga
    Emanuel Varga avatar
    1336 posts
    Member since:
    May 2010

    Posted 29 Apr 2011 Link to this post

    Hello again,

    That's why in my original post i've added that Rows.ToArray(), like so:
    For Each row1 As GridViewRowInfo In RadGridView1.Rows.ToArray()
        If row1.Cells("Statut").Value <> "b" Then row1.Delete()
    Next

    and just add a reference to System.Linq

    Hope this helps, if you have any other questions or comments, please let me know,

    Best Regards,
    Emanuel Varga

    Telerik WinForms MVP

  6. Hector
    Hector avatar
    25 posts
    Member since:
    Jul 2011

    Posted 19 Aug 2011 Link to this post

    I had a similar situation. I tried the following:

     

    For Each gvr As tlrk.GridViewRowInfo In Me.dgTotalCosts.Rows
        ' Delete rows that are NOT locked
        If (gvr.Cells("colLock").Value = False) Then
           gvr.Delete()
        End If
    Next

    ... and received the following exception:
    Collection was modified; enumeration operation may not execute.

    Per your suggestion I tried the following code:

    For Each gvr As tlrk.GridViewRowInfo In Me.dgTotalCosts.Rows.ToArray()
        ' Delete rows that are NOT locked
        If (gvr.Cells("colLock").Value = False) Then
           gvr.Delete()
        End If
    Next

    ... and it worked!!!

    However, i don't understand why. Can you explain why adding .ToArray() to the Rows collection fixed the issue? What happened behind the scenes that did not happen before? If .ToArray() converted the RadGridView's Rows collection, which implements System.Collections.IEnumerable(Of T) to an array, then how can we access gvr as a GridViewRowInfo object still?

    Incidentally, FYI ... I got the following error when I clicked the [>> Reply] button the first time:

    RadUpload Ajax callback error. Source url returned invalid content:

    Internal server error

    ../../Telerik.RadUploadProgressHandler.ascx?RadUrid=d8059694-9d4e-47dc-91c2-b1cee6405507

    Did you register the RadUploadProgressHandler in web.config?

    Please, see the help for more details: RadUpload for ASP.NET Ajax - Configuration - RadUploadProgressHandler.


    Thanks in advance for your help.
  7. Alexander
    Admin
    Alexander avatar
    306 posts

    Posted 24 Aug 2011 Link to this post

    Hello Hector,

    Thank you for your question.

    Your first code snippet is similar to the following code which tries to remove elements from a list using For Each loop:
    Dim List As New List(Of Integer)()
    List.Add(1)
    List.Add(2)
    List.Add(3)
    For Each Item As Integer In List
        ' if (condition)
        List.Remove(Item)
    Next

    This approach is not correct in .Net and raises the exception you observe.

    The second approach is similar to the following one:
    Dim List As New List(Of Integer)()
    List.Add(1)
    List.Add(2)
    List.Add(3)
    For Each Item As Integer In List.ToArray()
        ' if (condition)
        List.Remove(Item)
    Next

    In this approach the For Each loops the elements of the created array ( List.ToArra() ) and removes the elements of the initial list. As the loop is not related to the elements of the list, elements from the list could be removed.

    Thank you for your report concerning Reply button error. I have forwarded it to our Web team, so that they can investigate this case further.

    Best regards,
    Alexander
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  8. Fiko
    Admin
    Fiko avatar
    1406 posts

    Posted 24 Aug 2011 Link to this post

    Hi Hector,

    In reference to the error you experienced:

    "Incidentally, FYI ... I got the following error when I clicked the [>> Reply] button the first time:
    RadUpload Ajax callback error. Source url returned invalid content:
    Internal server error

    ../../Telerik.RadUploadProgressHandler.ascx?RadUrid=d8059694-9d4e-47dc-91c2-b1cee6405507
    Did you register the RadUploadProgressHandler in web.config?
    Please, see the help for more details: RadUpload for ASP.NET Ajax - Configuration - RadUploadProgressHandler"


    The RadUplaod's handler is correctly registered and I am not sure what is causing the problem on your side. If you still experience the same problem, could you please provide us with the exact reproduction steps? We have tried to reproduce the problem several times (on different machines as well) but unfortunately with no success.


    Best wishes,
    Fiko
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Back to Top
UI for WinForms is Visual Studio 2017 Ready