简体   繁体   English

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

[英]Delete row from DataGridView via button on the row

How can I delete a DataGridView row via a button on the row? 如何通过行上的按钮删除DataGridView行?

screenshot: http://i.imgur.com/8342MKT.png 屏幕截图: 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. 但是,尽管这适用于DataGridView外部的按钮,但不适用于DataGridView中的按钮。

How can I make this work with the button in the DataGridView? 如何使用DataGridView中的按钮使它起作用?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM