我想在按下删除键时清除所选行(由datagridcomboboxcell和textboxcell组成)。以下代码无效。
Private Sub dgvCanaraBankCAU_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvCanaraBankCAU.KeyDown
If e.KeyCode = Keys.Delete Then
Dim i As Integer = 0
For Each row As DataGridViewRow In dgvCanaraBankCAU.SelectedRows
dgvCanaraBankCAU.Rows(i).Cells(0).Value = ""
dgvCanaraBankCAU.Rows(i).Cells(1).Value = ""
dgvCanaraBankCAU.Rows(i).Cells(2).Value = ""
dgvCanaraBankCAU.Rows(i).Cells(3).Value = ""
dgvCanaraBankCAU.Rows(i).Cells(4).Value = ""
dgvCanaraBankCAU.Rows(i).Cells(5).Value = ""
dgvCanaraBankCAU.Rows(i).Cells(6).Value = ""
Next
End If
End Sub
答案 0 :(得分:0)
看来,发布的代码对行使用了错误的索引。在所选行的For Each
循环中,Row
索引为i
,其值永远不会从零变化。代码将始终清除行零(0)。下面对此索引的更改似乎按预期工作。
Private Sub dgvCanaraBankCAU_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dgvCanaraBankCAU.KeyDown
If e.KeyCode = Keys.Delete Then
For Each row As DataGridViewRow In dgvCanaraBankCAU.SelectedRows
dgvCanaraBankCAU.Rows(row.Index).Cells(0).Value = ""
dgvCanaraBankCAU.Rows(row.Index).Cells(1).Value = ""
dgvCanaraBankCAU.Rows(row.Index).Cells(2).Value = ""
dgvCanaraBankCAU.Rows(row.Index).Cells(3).Value = ""
dgvCanaraBankCAU.Rows(row.Index).Cells(4).Value = ""
dgvCanaraBankCAU.Rows(row.Index).Cells(5).Value = ""
dgvCanaraBankCAU.Rows(row.Index).Cells(6).Value = ""
Next
End If
End Sub