SCLEE KOREA
SCLEE KOREA

Reputation: 35

Delete row from DataGridView via button on the row

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?

Upvotes: 1

Views: 16927

Answers (1)

SSS
SSS

Reputation: 5413

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

Upvotes: 4

Related Questions