August 7th, 2003, 05:09 PM
DataGrid: Tabbing down rows ( not cells ) & Enter to Submit
I have a DataGrid. I need to do the following with this datagrid.
Tab will be the equivalent as pressing arrow down and will simply just increment the row of the CurrentRow.
Enter will trigger a button on the form.
Any insight would be much appreciated.
August 7th, 2003, 06:53 PM
1) Override the KeyPress event.
2) Check which key was pressed.
3) If it was enter or tab write code to do what you want it to do.
4) If enter or tab do anything else by default, set the e.Handled property to true.
Hope that helps,
August 15th, 2003, 03:37 PM
the only way i can override is if i make a separate datagrid class .. i'm not sure how to press a button on the target form though.
public class MyDataGrid : DataGrid
protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg, System.Windows.Forms.Keys keyData)
if(msg.WParam.ToInt32() == (int) Keys.Enter)
// triger a button outside this class
return base.ProcessCmdKey(ref msg, keyData);
August 15th, 2003, 04:34 PM
ok forget all this override stuff .. i just want to assign any key to trigger a button_OnClick() function.
F9 would do just fine .. i've tried this code, but still can't get it to trigger:
any insight is much appreciated.
private void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
if(e.KeyCode == Keys.F9)
August 15th, 2003, 09:22 PM
Use the datagrid's keydown event.
August 15th, 2003, 11:27 PM
if i start a new windows application, create a new datagrid, then add the handler and method below .. shouldn't it open a message box when i press f9 after being focused on the datagrid?
then i add this code:
this.dataGrid1 = new System.Windows.Forms.DataGrid();
// added handler
this.dataGrid1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.dataGrid1_KeyDown);
static void Main()
// added method
private void dataGrid1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
if(e.KeyCode == Keys.F9)
Last edited by sde; August 15th, 2003 at 11:30 PM.
August 15th, 2003, 11:51 PM
That's what I use, have you tried a key other than F9 because it should be working...
August 15th, 2003, 11:58 PM
thanks for clearing that up!!
it actually does work when i am not in a cell of the datagrid.
so what would be the proper way to override this so i could capture F9 even when inside a cell?
do i have to make a new datagrid class? or can i do it in this method?
i have tried putting 'override' in front of it, but it says that there is not a suitable method to override.