0
votes

can anyone help me to make the following code to delete all the "highlighted" cells(current cells) contents in my datagridview? it always deletes the content of the last selected cell only

Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
    If e.KeyData = Keys.Delete Then

        For Each cell In DataGridView1.SelectedCells
            DataGridView1.CurrentCell.Value = ""
        Next
    End If
End Sub
2

2 Answers

1
votes
Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
    If e.KeyData = Keys.Delete Then

        For Each cell In DataGridView1.SelectedCells
            cell.Value = ""
        Next
    End If
End Sub

note you're enumerating through all the selected cells (look at For Each cell In...), which sets cell to each selected DataGridViewCell in turn. That's when you blank out the value.

Your code simply blanked out CurrentCell once for every selected cell. You should brush up on how iterators and For Each statements work.

0
votes

In your For loop you are not referencing the iteration variable "Cell" - you are only referencing the "CurrentCell" which doesn't change as you iterate.