0

This is such a basic question, I'm almost embarassed to ask it. How do I loop through seleted rows in a DataGridView and remove the rows?

I've tried this, but it doesn't work. After one row is removed, the index is all screwed up...

iIndex = dgvRecipe.Rows.GetNextRow(-1, DataGridViewElementStates.Selected)
            While iIndex <> -1
                dgvRecipe.Rows.RemoveAt(iIndex)
                iIndex = dgvRecipe.Rows.GetNextRow(iIndex, DataGridViewElementStates.Selected)
            End While

Edited by tim8w: n/a

2
Contributors
2
Replies
31
Views
6 Years
Discussion Span
Last Post by bhagawatshinde
0

As jwavila reminded me on another forum...

For iIndex = dgvRecipe.RowCount - 1 To 0 Step -1
    dgvRecipe.Rows.RemoveAt(iIndex)
Next iIndex
0

Try this

For Each dr As DataGridViewRow In dtgqueformat.SelectedRows    ' delete selected records
               If (dtgqueformat.Rows.Count > 1) Then
                     dtgqueformat.Rows.Remove(dr)
               End If
      Next

Edited by bhagawatshinde: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.