通过行上的按钮从DataGridView中删除行

时间:2015-07-21 01:09:32

标签: vb.net datagridview

如何通过行上的按钮删除DataGridView行?

屏幕截图:http://i.imgur.com/8342MKT.png

当用户点击某行的“삭제”按钮时,我想删除该行。

我试过了:

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

但是虽然这适用于DataGridView外部的按钮,但它不适用于DataGridView中的按钮。

如何使用DataGridView中的按钮使其工作?

1 个答案:

答案 0 :(得分:2)

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