I've a datagridview, whenever I display the master record, the associated children records are displayed in the datagridview. Bbefore filling in the children records into the datagridview again, I'll first clear all rows in the datagridview.

I first tried using the following statement and it did not work. I got an error msg: "Index out of range." Mus be non-negative and less than the size of the collection. There are 3 rows of data plus a blank row (at the bottom); hence Me.dgvMaterial_Inventory.Rows.Count = 4.

Me.dgvMaterial_Inventory.Rows.RemoveAt(dgvMaterial_Inventory.SelectedRows(dgvMaterial_Inventory.Rows .Count - 1).Index)

I then tried one row at a time using a For-loop as shown below
However, I got another error indicating "Uncommitted new row cannot be deleted." when trying to delete the 2nd row (with i=1).

Can someone please tell me why ? If this is not the correct way to do it, can you please advise what is the easy way to delete ALL rows.

---------------
VB Code
Private Sub Clear_DataGridView()
Dim i As Integer

For i = 0 To Me.dgvMaterial_Inventory.Rows.Count - 1
Me.dgvMaterial_Inventory.Rows(i).Selected = True
Me.dgvMaterial_Inventory.Rows(i).Dispose()
Me.dgvMaterial_Inventory.Rows.RemoveAt(Me.dgvMaterial_Inventory.SelectedRows(0).Index)
Next
End Sub