We help IT Professionals succeed at work.

Taking a DataGridView Cell Out of Edit Mode

kiranboi
kiranboi asked
on
1,173 Views
Last Modified: 2012-08-14
Hi all,

I have a DataGridView in my app that I need to behave in various ways depending on the users input. The problem I am having at the moment is if I use the keydown event and check to see if the tab key has been pressed this only worksif the cell isn't in edit mode. Is there anyway I can force the cell out of edit mode programatically so I can then work with the events?

Thanks
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Use the DataGridView.EndEdit method.

Bob

Author

Commented:
that didnt work. below is the code i am using, it is triggered if a cell in column 3 of my dgv is selected and tab is pressed:

                    dgvMain.EndEdit()
                    If dgvMain.Rows(dgvMain.SelectedCells(0).RowIndex).Cells(0).Value = "-" Then
                        dgvMain.Rows(dgvMain.SelectedCells(0).RowIndex).Cells(9).Selected = True
                        dgvMain.Select()
                    End If

If I am not editing cell 3 it works fine but is still not triggering the code if the cell is in edit mode when tab is pressed
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Where are you calling that code from?

Bob

Author

Commented:
the dgv's keydown event. its then checking the e.keycode and if its tab its running the above code
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
When you are in the edit mode, then the DataGridView's KeyDown event doesn't fire.  You would have to add a KeyDown event handler to the control that is available in the EditingControlShowing event handler.

Bob

Author

Commented:
any chance of an example of how to do that?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Commented:
One note: you should avoid adding handler more than once, so there should be some sort of check if it is already added, or you should first remove handler and then add it.

Goran

Author

Commented:
I've tried using that but it still is'nt causing the DataGridView's KeyDown event to trigger
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Did you think that it would trigger the DataGridView.KeyDown event?  It will trigger the TextBox control's KeyDown.

Bob

Author

Commented:
Sorry I'm getting confused. I've had a play around with that code but I'm still not sure how I can use it.
Can you give me an example of how I can make a custome Sub trigger when Tab is pressed when a DataGridView cell is in edit mode?
Thanks
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
That was a pretty direct example of what I think that you should do, so I don't understand what your confusion is.  How can I explain that process so that you can at least test out the theory?

Bob

Author

Commented:
Well Ive inserted your code into my app and it doesn't seem to make a difference. Where, for example, would I insert a message box line of code so that the message box would popup if tab is pressed when a DataGridView cell is in edit mode?

Commented:
It seems that Tab is not passed to KeyDown event. To catch the tab (the only way I know if) you would need to create your own DataGridView and override the ProcessCmdKey procedure. And then you would use this class instead of DataGridView class. Overriding would look like this:


    class CustomDataGridView : DataGridView
    {
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            Keys key = keyData & Keys.KeyCode;

            if (this.CurrentCell != null)
            {
                if (this.CurrentCell.IsInEditMode)
                {
                    switch (key)
                    {
                        case Keys.Tab:
                            MessageBox.Show("Tab pressed");
                            break;
                    }
                }
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }
    }

Goran

Commented:
One way or another, you will in time find out that you must change behavoir of datagridview, so you will end up with using a cusomized one in your app. The example I gave is only for testing purposes, to see that it works. You should put this code in a dll, which will allow you to have CustomDataGridView in ToolBox, so you can drop it on a form like any other control.

Goran

Author

Commented:
Bob,
thanks for the help. the code you gave me still doesn't trigger when tab is pressed but it works for all the other keys and I'll be able to make use of that.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.