6 Answers, 1 is accepted
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
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
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
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.
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 >>
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 >>