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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31528768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 19:18:54  来源:igfitidea点击:

Delete row from DataGridView via button on the row

vb.netdatagridview

提问by SCLEE KOREA

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 中的按钮进行此操作?

回答by SSS

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