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

As jwavila reminded me on another forum...

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

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.