Results 1 to 9 of 9

Thread: DataGridView Events

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2018
    Posts
    62

    Question DataGridView Events

    I've created a tiny app to test events from an unbound DGV. I've never used the dgv before and want to see which events happen when, all the test app does is to debug print which event happens so the only code is a sequence of event handlers. I've got most of the common Cell events covered such as CellLeave, CellEnter. etc., plus grid events Keydown, up and press. It's very important that I can detect keystrokes because the real app has to change some of them (for example from lower to upper) and also tell when shift was pressed with a char.
    I start in cell 0,0, press tab followed by key a, followed by another tab, I get the following:
    Key Down
    CellLeave
    CellValidating
    CellEnter
    Key Press (tab)
    Key Up (tab)
    CellBeginEdit (this is the 'a' press)
    CellLeave (the 2nd tab)
    CellValidating
    CellValueChanged
    CellEndEdit
    CellEnter
    Key Up (the 2nd tab)
    But this sequence is useless, as soon as I press 'a' and get CellBeginEdit it stops reporting keys, I can type away for ever and it never generates an event to report keypresses, until I tab out of the cell.
    Is this a bug?
    Is there any way to get the missing keystrokes?

    Thanks for any help
    Ian
    Last edited by IanBrooke; Mar 14th, 2018 at 10:10 AM.

  2. #2
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: DataGridView Events

    You will want to read this

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: DataGridView Events

    Why does the real app have to change some of them, and why is it important to happen when the key is pressed? Normally, you let the user enter what they will, then use the Validating event to decide whether what they wrote is appropriate. You could switch the case of strings in there, or tell the user they are out of line and not let them leave till they've fixed it. If you are actually doing something on keypress, then you are counting on them not to make a mistake, which you really can't count on.
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2018
    Posts
    62

    Re: DataGridView Events

    The user is able to select a formatting mode on a column basis (via a menu option), these, for example, can select the column be all upper-case, or name-case etc, the user can then type all in lower-case and the program will correct it for them as they go along, these are inexperienced users who may have no typing skill. I agree that the program could change the entire cell in Validating, except for the shift key, this key is used to reverse the normal formatting on the next character, as an example if using name-case the program would change mccartney to Mccartney but by shifting the 2nd c the user can make the program change it to McCartney and there's no way that I can think of to detect, after the event, that shift had been pressed on that 2nd c.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: DataGridView Events

    Well, that certainly answered my question. Given what you are trying to do, I'd say you have it worked out about right. How does that event kpmc work for you? It's an interesting event relative to the others you listed, cause it seems to happen pretty doggone early.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2018
    Posts
    62

    Re: DataGridView Events

    Thanks.
    I did look at kpmc, so far didn't get it to work, the difficulty is that as soon as the cell goes into editmode it stops notifying keypresses which seems to me pretty stupid, there doesn't seem to be a simple solution.
    I did find this: https://social.msdn.microsoft.com/Fo...msdatacontrols
    and there's some really useful stuff which looks promising. It's all very silly, this is a rewrite of a vb6 app and it was fairly straightforward back then to do this using an MSFlexgrid and dropping a textbox on the cell and the best solutions, including those suggested by MS in that link, involve creating a handler for a textbox! The old ways still seem to be the best!
    Thanks for the help, I'll see how I get on with handlers and a textbox.

  7. #7
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: DataGridView Events

    Quote Originally Posted by Shaggy Hiker View Post
    Well, that certainly answered my question. Given what you are trying to do, I'd say you have it worked out about right. How does that event kpmc work for you? It's an interesting event relative to the others you listed, cause it seems to happen pretty doggone early.
    You dont want to put all your code in the CurrentCellDirtyStateChanged Event. Your code should be in the CellValueChanged event.

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

    Re: DataGridView Events

    A DataGridView contains no child controls by default. When you start editing a cell, an editing control is created and embedded in the cell. That is why you stop seeing events raised by the grid while you're editing: it's the editing control that is raising events. When the editing control is created, the Value of the cell is assigned to the appropriate property of the control, e.g. the Text of a TextBox. When the editing session ends, the control property is assigned back to the Value property of the cell. That's why you don't see a CellValueChanged event from the grid until you end the editing session, which is usually done by navigating to a different cell.

    What you can do is handle the EditingControlShowing event of the grid to get access to the editing control. You can then attach the appropriate event handlers to it. For instance, if you need to process keyboard input when editing a text box cell, you can attach handlers for KeyDown and/or KeyUp and/or KeyPress of the TextBox editing control. You can then handle the CellEndEdit event of the grid to detach those event handlers.
    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
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: DataGridView Events

    Quote Originally Posted by jmcilhinney View Post
    A DataGridView contains no child controls by default. When you start editing a cell, an editing control is created and embedded in the cell. That is why you stop seeing events raised by the grid while you're editing: it's the editing control that is raising events. When the editing control is created, the Value of the cell is assigned to the appropriate property of the control, e.g. the Text of a TextBox. When the editing session ends, the control property is assigned back to the Value property of the cell. That's why you don't see a CellValueChanged event from the grid until you end the editing session, which is usually done by navigating to a different cell.

    What you can do is handle the EditingControlShowing event of the grid to get access to the editing control. You can then attach the appropriate event handlers to it. For instance, if you need to process keyboard input when editing a text box cell, you can attach handlers for KeyDown and/or KeyUp and/or KeyPress of the TextBox editing control. You can then handle the CellEndEdit event of the grid to detach those event handlers.
    I did not know that. That's the solution that had to be used in VB6 because the MSFlexGrid was read only. You had to do it yourself, though. When the user clicked on the cell, you moved and sized a textbox to the size and location of the cell, then copied the cell contents into the textbox. If you did it right, the user never knew they weren't editing in the cell. MS formalized that approach for the DGV, though they also extended it a bit.
    My usual boring signature: Nothing

Tags for this Thread

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