DataGridView.Rows.RemoveAt problems

wingless

Member
Joined
Jun 28, 2007
Messages
10
Programming Experience
1-3
Hi, I was trying to do a code that would erase my DataGridView rows, but I can't seem to be able to do it. If anybody could help it would be very cool.
Thanks :)

Code:

VB.NET:
Expand Collapse Copy
Private Sub searchClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles searchClear.Click
  Dim i As Integer
  For i = 0 To DataGridView1.RowCount - 2
    DataGridView1.Rows.RemoveAt(i)
  Next
End Sub

I get the following exception:

VB.NET:
Expand Collapse Copy
InvalidOperationException
Impossible de supprimer la nouvelle linge non validée

which in English is something like:
Unable to delete the new unvalide line
 
VB.NET:
Expand Collapse Copy
DataGridView1.Rows.Clear()
Also one tip, if iterating a collection and removing items at a index DO iterate from higher index and Step backwards. If not the collection will change with the first index removed while your indexer continues its loop upwards, which simply don't work.
 
Hi, thanks for answering quickly.

Thanks for the tip on iterating DataGrids, hadn't thought of the indey changes.

Anyways I tried : Da
VB.NET:
Expand Collapse Copy
taGridView.Rows.Clear()

But now I get this exception :

System.ArgumentException :
Impossible d'effacer cette liste
which in English is something like:
Unable to delete this list.

Do you have an idea?
 
Sounds as you have bound the grid control to a datatable or something, in which case you modify the source instaed.
 
Yeah, sorry I forgot to mention that...didn't think it would affect.

My grid is indeed bound to a datatable, which itself is filled by an Access query...

Thanks for the help!
 
Back
Top