kiranboi
asked on
Taking a DataGridView Cell Out of Edit Mode
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
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
ASKER
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.Selec tedCells(0 ).RowIndex ).Cells(0) .Value = "-" Then
dgvMain.Rows(dgvMain.Selec tedCells(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
dgvMain.EndEdit()
If dgvMain.Rows(dgvMain.Selec
dgvMain.Rows(dgvMain.Selec
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
Where are you calling that code from?
Bob
Bob
ASKER
the dgv's keydown event. its then checking the e.keycode and if its tab its running the above code
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
Bob
ASKER
any chance of an example of how to do that?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Goran
ASKER
I've tried using that but it still is'nt causing the DataGridView's KeyDown event to trigger
Did you think that it would trigger the DataGridView.KeyDown event? It will trigger the TextBox control's KeyDown.
Bob
Bob
ASKER
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
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
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
Bob
ASKER
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?
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.IsInEdit Mode)
{
switch (key)
{
case Keys.Tab:
MessageBox.Show("Tab pressed");
break;
}
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
Goran
class CustomDataGridView : DataGridView
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
Keys key = keyData & Keys.KeyCode;
if (this.CurrentCell != null)
{
if (this.CurrentCell.IsInEdit
{
switch (key)
{
case Keys.Tab:
MessageBox.Show("Tab pressed");
break;
}
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
Goran
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
Goran
ASKER
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.
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.
Bob