This is a migrated thread and some comments may be shown as answers.

delete rows on condition

6 Answers 387 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Filleau
Top achievements
Rank 1
Filleau asked on 29 Apr 2011, 07:08 PM
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...

6 Answers, 1 is accepted

Sort by
0
Emanuel Varga
Top achievements
Rank 1
answered on 29 Apr 2011, 07:44 PM
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
0
Filleau
Top achievements
Rank 1
answered on 29 Apr 2011, 08:11 PM
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
0
Accepted
Emanuel Varga
Top achievements
Rank 1
answered on 29 Apr 2011, 09:05 PM
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

0
Hector
Top achievements
Rank 1
answered on 19 Aug 2011, 01:22 PM

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.
0
Alexander
Telerik team
answered on 24 Aug 2011, 07:31 AM
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 >>

0
Fiko
Telerik team
answered on 24 Aug 2011, 11:41 AM
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 >>

Tags
GridView
Asked by
Filleau
Top achievements
Rank 1
Answers by
Emanuel Varga
Top achievements
Rank 1
Filleau
Top achievements
Rank 1
Hector
Top achievements
Rank 1
Alexander
Telerik team
Fiko
Telerik team
Share this question
or