|
-
Jul 17th, 2007, 01:06 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2005] KeyDown - KeyPress - Delete Cell Value
When the delete key is pressed, I want to set the value of the current cell's text to "".
I tried this in the KeyDown and KeyPress events. Neither worked. (I expected it to work for any key, not just Delete.) It does nothing visible.
vb Code:
Private Sub grdG1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles grdG1.KeyPress
Dim grd As DataGridView = DirectCast(sender, DataGridView)
grd.CurrentCell.Value = ""
End Sub
By the way, is Nothing the same as ""? Or could the first one cause Null error when the 2nd doesn't?
-
Jul 17th, 2007, 02:03 AM
#2
Re: [2005] KeyDown - KeyPress - Delete Cell Value
Nothing is not the same as an empty string, but in many circumstances they have the same effect. If a string is a piece of paper then an empty string is an empty piece of paper while Nothing is no paper at all. Regardless, if you want to specify an empty string you should use the String.Empty property.
-
Jul 17th, 2007, 02:25 AM
#3
Thread Starter
Hyperactive Member
Re: [2005] KeyDown - KeyPress - Delete Cell Value
Ok that's cleared up now. But what about setting the grid's cell.value to String.Empty when the Delete Key is pressed?
-
Jul 17th, 2007, 02:35 AM
#4
Re: [2005] KeyDown - KeyPress - Delete Cell Value
The Delete key doesn't raise a KeyPress event, as many keys do not. You say you tried the keyDown event but this just worked fine for me:
vb.net Code:
Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
If e.KeyCode = Keys.Delete Then
Me.DataGridView1.CurrentCell.Value = String.Empty
End If
End Sub
-
Jul 17th, 2007, 02:43 AM
#5
Thread Starter
Hyperactive Member
Re: [2005] KeyDown - KeyPress - Delete Cell Value
I tried identical code in my app just now. I know my Delete key works but not in this code.
vb Code:
Private Sub grdG1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles grdG1.KeyDown
If e.KeyCode = Keys.Delete Then
grdG1.CurrentCell.Value = String.Empty
End If
End Sub
-
Jul 17th, 2007, 04:05 AM
#6
Re: [2005] KeyDown - KeyPress - Delete Cell Value
What type of data is in the cell? Is it a string or some other type? If it's some other type then setting its value to an empty string wouldn't make sense.
-
Jul 17th, 2007, 04:22 AM
#7
Thread Starter
Hyperactive Member
Re: [2005] KeyDown - KeyPress - Delete Cell Value
-
Jul 17th, 2007, 08:28 AM
#8
Re: [2005] KeyDown - KeyPress - Delete Cell Value
Is your cell in edit mode? If so then the grid probably isn't raising any KeyDown event because it's the editing control that does.
-
Jul 17th, 2007, 12:26 PM
#9
Thread Starter
Hyperactive Member
Re: [2005] KeyDown - KeyPress - Delete Cell Value
I'm not editing the cell, just pressing Delete when I enter the cell. The Grid's EditMode is set to EditOnKeystrokeOrF2.
I tried this code in the KeyDown even and it didn't bring up anything.
-
Jul 18th, 2007, 01:22 AM
#10
Thread Starter
Hyperactive Member
Re: [2005] KeyDown - KeyPress - Delete Cell Value
I must have had an error somewhere else. It's working now after I edited some other stuff.
-
Jul 18th, 2007, 10:31 AM
#11
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2005] KeyDown - KeyPress - Delete Cell Value
How would I then fire the CellEndEdit from the KeyDown even? The sender paramaters would be the same but the 2nd paramaters are different.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|