1

How can I delete a DataGridView row via a button on the row?

screenshot: https://i.sstatic.net/ofynP.png

When the user clicks the '삭제' button on a row, I want to delete that row.

I tried this:

For Each row As DataGridViewRow In DataGridView1.SelectedRows
  DataGridView1.Rows.Remove(row)
Next

But although this worked for a button outside the DataGridView, it doesn't work with a button in the DataGridView.

How can I make this work with the button in the DataGridView?

1
  • the row doesnt need to be selected for the button to be clicked. look at the event args Commented Jul 21, 2015 at 1:24

1 Answer 1

4
Option Strict On
Option Explicit On   

Public Class Form1
  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For i As Integer = 0 To 5
      DataGridView1.Rows.Add(i.ToString, i.ToString, i.ToString, "삭제")
    Next
  End Sub

  Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    If e.ColumnIndex = 3 Then
      DataGridView1.Rows.RemoveAt(e.RowIndex)
    End If
  End Sub
End Class
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.