Click here to Skip to main content
15,886,075 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Professionals,

After making a thorough search, I came to conclusion that best way to handle TAB key on gridview was to override the ProcessCmdKey(ref Message msg, Keys keyData) of the gridview control in my custom class.

This is my custom class.

C#
public partial class GridViewForKeyPress : DataGridView
{
    public static Keys KeyPressed;

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        KeyPressed = keyData;
        return base.ProcessCmdKey(ref msg, keyData);
    }
}


I have added this control to my form which contains my GridViewForKeyPress Control. In the form my code on the control's events :

C#
private void gvOutletRates_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
    TextBox txtrate = e.Control as TextBox;
    txtrate.KeyDown -= txtrate_KeyDown;
    txtrate.KeyDown += txtrate_KeyDown;
}

void txtrate_KeyDown(object sender, KeyEventArgs e)
{
    MessageBox.Show(MyCustomControls.GridViewForKeyPress.KeyPressed.ToString());
}



On a key press,

For alphanumeric keys, Backspace, Space etc., MessageBox shows the KeyCode.

When Enter Key is pressed, No Message but control moves to next cell.

When Tab key is pressed, the cell is selected. No Message.

Where am I going wrong, or is the logic totally wrong ? How to correctly trap the TAB key on the Grid View. Please suggest.

Thanks in advance. I am a learner. Please excuse any unskilled logic procedures.
Posted
Updated 6-Feb-15 0:56am
v2
Comments
BillWoodruff 6-Feb-15 6:44am    
What do you want to DO when the Tab key is pressed in a DataGridView ? The code you show does not use the static 'KeyPressed variable you define in your custom DataGridView, so it's impossible to say where you might be going "wrong."
Priya-Kiko 6-Feb-15 6:55am    
Thanks for the immediate response.

When TAB key is pressed, I want to disable the gridview and proceed with updation code.

Sorry Edited :

void txtrate_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show(MyCustomControls.GridViewForKeyPress.KeyPressed.ToString());
}

1 solution

The Form is capturing the TAB key. That is how a Windows form normally works to change focus to the next control within the Form's tab order.

So..., you have to change how the WinForm object ( not the DataGridView ) handle the TAB key:
C#
private bool isTabCaptureOn = false;
protected override bool ProcessTabKey(bool forward)
{
    if (isTabCaptureOn)
    {
        return true;
    }
    else
    {
        return base.ProcessTabKey(forward);
    }
}


When isTabCaptureOn is true, the form will allow you to assign and work with the DataGridView's KeyUp() method to process the TAB key in the fashion that you are seeking. However, you will not be able to TAB out of the control to another control unless you develop a method to turn capture off again.

I'll leave that exercise to you....
 
Share this answer
 
v2
Comments
Priya-Kiko 7-Feb-15 2:25am    
Thank you So much.
I worked on that and got lot of things cleared. Thanks again.
DelphiCoder 7-Feb-15 4:11am    
Happy to help. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900