Vb.net – Delete row from DataGridView via button on the row

datagridviewvb.net

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

screenshot: http://i.imgur.com/8342MKT.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?

Best Answer

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
Related Topic