Results 1 to 11 of 11

Thread: [RESOLVED] [2005] KeyDown - KeyPress - Delete Cell Value

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Resolved [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:
    1. Private Sub grdG1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles grdG1.KeyPress
    2.         Dim grd As DataGridView = DirectCast(sender, DataGridView)
    3.         grd.CurrentCell.Value = ""
    4.     End Sub

    By the way, is Nothing the same as ""? Or could the first one cause Null error when the 2nd doesn't?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    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?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Private Sub DataGridView1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DataGridView1.KeyDown
    2.     If e.KeyCode = Keys.Delete Then
    3.         Me.DataGridView1.CurrentCell.Value = String.Empty
    4.     End If
    5. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    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:
    1. Private Sub grdG1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles grdG1.KeyDown
    2.         If e.KeyCode = Keys.Delete Then
    3.             grdG1.CurrentCell.Value = String.Empty
    4.         End If
    5.     End Sub

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    Re: [2005] KeyDown - KeyPress - Delete Cell Value

    Yep. It's a string.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    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.
    vb Code:
    1. EditOnKeystrokeOrF2

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    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.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Fox, OK
    Posts
    381

    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
  •  



Click Here to Expand Forum to Full Width