vb.net 在VB.NET中删除DataGridView控件中的一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20200315/
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
Delete a row in DataGridView Control in VB.NET
提问by user123456789
I am working on DataGridView control in vb.net. I need help you that i want to delete a row in datagrid and only that row deleted who selected. Mean first i select the row then row deleted. So please provide me code that how i select and delete row from DataGridView control in VB.NET
我正在 vb.net 中处理 DataGridView 控件。我需要帮助你,我想删除数据网格中的一行,并且只有那一行删除了谁选择了。意思是首先我选择行然后删除行。所以请提供我如何从 VB.NET 中的 DataGridView 控件中选择和删除行的代码
thank
谢谢
回答by AdorableVB
For Each row As DataGridViewRow In yourDGV.SelectedRows
yourDGV.Rows.Remove(row)
Next
This will delete all rows that had been selected.
这将删除所有已选择的行。
回答by NoChance
Assuming you are using Windows forms, you could allow the user to select a row and in the delete key click event. It is recommended that you allow the user to select 1 row only and not a group of rows (myDataGridView.MultiSelect = false)
假设您使用的是 Windows 窗体,您可以允许用户在删除键单击事件中选择一行。建议您只允许用户选择 1 行而不是一组行 (myDataGridView.MultiSelect = false)
Private Sub pbtnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
If myDataGridView.SelectedRows.Count > 0 Then
'you may want to add a confirmation message, and if the user confirms delete
myDataGridView.Rows.Remove(myDataGridView.SelectedRows(0))
Else
MessageBox.Show("Select 1 row before you hit Delete")
End If
End Sub
Note that this will not delete the row form the database until you perform the delete in the database.
请注意,在您在数据库中执行删除之前,这不会从数据库中删除行。