DataGridView KeyDown?

(VS2005 - beta 2)

Hello, is there anyway to capture a keypress or keydown event in the DataGridView? I know there's events called KeyDown and Keypress, but if the DGV is in edit mode, they never get caught.

My ultimate goal is to make it so pressing enter moves to the Cell to the right, instead of to the next row but I need to know what key is being pressed before I can do that.

Thanks
-Adam

[406 byte] By [AdamPlocher] at [2007-12-22]
# 1
I do want to know the keypress/keydown event for datagrid or view. Any Help please. Thanks. Jil
Jil at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
Ive the same problem. Its ridiculous that such improved control cant do that simple thing. Ive tried a lot of ways such Overriden ProcessCMDKeys and nothing. Also ive been searching for help in a lot of forums and nothing.

It would be great if someone could tell if theres anyway of do that.

MacKraken at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

To handle the KeyDown and KeyPress events for the editing control just handle the EditingControlShowing event and access the events on the editing control. Check out the DataGridView faq for more info: http://www.windowsforms.net/Samples/Go%20To%20Market/DataGridView/DataGridView%20FAQ.doc

That said, you do have to derive from the DataGridView to do what you want. This is due to the way that keyboard handling works for contained controls. In a future version of the DataGridView we would like to add a few more events (yeah!) to help make this scenario easier to do.
Here is the code you need to make the Enter key move the focus to the right:

public class dgv : DataGridView
{
protected override bool ProcessDialogKey(Keys keyData)
{
Keys key = (keyData & Keys.KeyCode);
if (key == Keys.Enter)
{
return this.ProcessRightKey(keyData);
}
return base.ProcessDialogKey(keyData);
}
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
return this.ProcessRightKey(e.KeyData);
}
return base.ProcessDataGridViewKey(e);
}
}

-mark

DataGridView Program Manager

Microsoft

This post is provided “as-is”

MarkRideout at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
Thank you very much Mark. It works great!.

As i said its not normal for a user that has to fill a lot of fields to have the enter key with that default behavior. Its not a big thing but theres nothing published on the web about this matter. thank you again.

MacKraken at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

I'll get the docs and DataGridView FAQ updated with this.

-mark

DataGridView Program Manager
Microsoft
This post is provided "as-is"

MarkRideout at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6

This modified version also adds a new row automatically and hops back to the first column after an enter given in the last column a row higher:



public class DataGridViewEnter : DataGridView
{
protected override bool ProcessDialogKey(Keys keyData)
{
Keys key = (keyData & Keys.KeyCode);
if (key == Keys.Enter)
{
return this.ProcessRightKey(keyData);
}
return base.ProcessDialogKey(keyData);
}

public new bool ProcessRightKey(Keys keyData)
{
Keys key = (keyData & Keys.KeyCode);
if (key == Keys.Enter)
{
if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.CurrentCell.RowIndex == (base.RowCount - 1)))
{
((BindingSource)base.DataSource).AddNew();
base.CurrentCell = base.Rows[base.RowCount - 1].Cells[0];
return true;
}

if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.CurrentCell.RowIndex + 1 != base.NewRowIndex))
{
base.CurrentCell = base.Rows[base.CurrentCell.RowIndex + 1].Cells[0];
return true;
}
return base.ProcessRightKey(keyData);
}
return base.ProcessRightKey(keyData);
}

protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
return this.ProcessRightKey(e.KeyData);
}
return base.ProcessDataGridViewKey(e);
}

Hope you guys find it usefull.

Cheers,

Paul

PaulDiterwich at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 7
This is handy. But I am having a problem if there is bad data in the last cell of the grid because the DataError event isn't firing when the base.CurrentCell is being changed. Any ideas on how to fix that?

Thanks,

Kevin

KevinBeck at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 8

I'm really stuggling with the visual basic documentation on trying to understand how to capture the key events when accessing the editing control of a DataGridView. Using a standard text column, I want to filter out letters and be able to capture numbers only, right, left, enter, tab, delete, escape, etc keys to create the behavior I would like.

Could you please provide exactly how I would use the e.control command to determine the editing control. I've tried using the calendar example and tried editing for a texbox, to no avlail.

New_to_Vb_studio at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 9
I want to move focus to next cell. I am using DataGridView control for DataEntry. So, When user done with editing particular cell and press enter, focus should go on to the next cell (not next row). How could I do this? please developers help me.. I am looking for this from last 10 days. Not got the solution yet
LoganX3 at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 10

There is a solution posted by me and others that creates the functionality that you are searching for. Or is it not applicable to you situation?

Cheers,

Paul

PaulDiterwich at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 11

Adjusted version where the last cell is also being checked...

public class DataGridViewEnter : DataGridView

{
//This override causes the DataGridView to use the enter key in a similar way as
//the tab key


protected override bool ProcessDialogKey(Keys keyData)
{
Keys key = (keyData & Keys.KeyCode);

if (key == Keys.Enter)
{
return this.ProcessRightKey(keyData);
}
return base.ProcessDialogKey(keyData);
}


public new bool ProcessRightKey(Keys keyData)
{
Keys key = (keyData & Keys.KeyCode);

if (key == Keys.Enter)
{
if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.CurrentCell.RowIndex == (base.RowCount - 1)))
{

//This causes the last cell to be checked for errors
base.EndEdit();
((BindingSource)base.DataSource).AddNew();
base.CurrentCell = base.Rows[base.RowCount - 1].Cells[0];
return true;
}

if ((base.CurrentCell.ColumnIndex == (base.ColumnCount - 1)) && (base.CurrentCell.RowIndex + 1 != base.NewRowIndex))
{
base.CurrentCell = base.Rows[base.CurrentCell.RowIndex + 1].Cells[0];
return true;
}
return base.ProcessRightKey(keyData);
}
return base.ProcessRightKey(keyData);
}


protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{

if (e.KeyCode == Keys.Enter)
{
return this.ProcessRightKey(e.KeyData);
}
return base.ProcessDataGridViewKey(e);
}

}

PaulDiterwich at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 12
I'm looking for visual basic programming. There is several postings dealing with C#. I've tried using some of the C# to visual basic converters but don't seem to get it to work. I'm not wanting to learn a copmplete new lanquage and well as learn a new editing interface. Is there a document that can aid Visual Basic users on this new control? I've tried using the calendar sample and replaced the dateTimePicker with a textbox. But, the text box is not moved or sized in the DataGridView cell. So, obviously I'm missing something. I want to capture any keydown event while the cell is in the editing mode.
New_to_Vb_studio at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 13
What happens when a column is not visible? The last code snippet doesn't work. Any ideas?
Babski at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 14

Hi Logan

I would explain step by step

You have to derive the datagridview class to do this.

First you have to add a new class1.vb to your project and write some code in it:

Public Class DGVMod

'this tells this class that is a datagridview
Inherits System.Windows.Forms.DataGridView

'this function do the half of job
'when you hit a key (like return) it transforms like you
'have been hit the tab key
'this doent work when you are editing the cell

Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean

If keyData = Keys.Return Then
keyData = Keys.Tab
With msg
.WParam = Keys.Tab
End With
End If

Return MyBase.ProcessCmdKey(msg, keyData)
End Function

'this second function works when you are editing the cell
'does the same as the function above changing return for tab
Protected Overrides Function ProcessDialogKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean

If keyData = Keys.Return Then
keyData = Keys.Tab
End If

Return MyBase.ProcessDialogKey(keyData)
End Function

End Class

Compile your project. You have to drag your new DGVMod control from the toolbox to your form and its done.

MacKraken at 2007-8-30 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...