我正在嘗試洗掉我的 DataTable 或 DataViewGrid 中的一行并且它可以作業,但是如果我選擇該行上的任何位置而不是整行本身,它會引發錯誤并且代碼停止。有沒有辦法做到這一點,所以當我選擇行的一部分并點擊洗掉時,整行都會被洗掉,或者至少是一個 If Else 陳述句,這樣如果沒有選擇整行,代碼就不會崩潰?
這是我在單擊按鈕時洗掉行的代碼。
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim i As Integer = DataGridView1.SelectedRows(0).Index
DataGridView1.Rows.RemoveAt(i)
End Sub
uj5u.com熱心網友回復:
您可以使用以下代碼洗掉 DataGridView 中的行。
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If DataGridView1.Rows.Count > DataGridView1.CurrentCell.RowIndex 1 Then
DataGridView1.Rows.RemoveAt(DataGridView1.CurrentCell.RowIndex)
End If
End Sub
或者,如果您想按“洗掉”鍵洗掉該行:
Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown
If e.KeyCode = Keys.Delete Then
If DataGridView1.Rows.Count > DataGridView1.CurrentCell.RowIndex 1 Then
DataGridView1.Rows.RemoveAt(DataGridView1.CurrentCell.RowIndex)
End If
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/428035.html
