DataGrid and the "Enter" Key pressed...

  • Thread starter Thread starter Byron McClain
  • Start date Start date
B

Byron McClain

.... is being consumed. I added an event handler for the "keypress" event
and my delegate
never gets executed. Why? I am trying to catch the "enter" key pressed
event to prevent
the DataGrid from going to currentRow + 1. Instead I want it to give focus
to the next cell (currentCol + 1) to the
right if not at the end of the row.

Example:

/**
* I have a grid with 3 columns. As the user enters in data and presses
"enter" ('\r') key
* I want the cell focus to move to the right instead of down (row + 1,
currentCol).
* Can this even be done?
*/

DataGridCell current= dataGrid.CurrentCell;

if(current.ColumnNumber < 2)
dataGrid.CurrentCell= new
DataGridCell(current.RowNumber,current.ColumnNumber + 1)
// else by default if I do nothing it should create a new row and put me at
the first cell.



Thanks,

Byron
 
Byron,

I guess that the keypress event for the datagrid is not fired because you're
actually in a textbox control (when pressing 'enter' in a datagrid cell).
I found the next code which may help you in an inherited datagrid to swap
the enter key with the tab key (which should move focus to the next cell)

protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg,
System.Windows.Forms.Keys keyData)
{
if(msg.WParam.ToInt32() == (int) Keys.Enter)
{
SendKeys.Send("{Tab}");
return true;
}
return base.ProcessCmdKey(ref msg, keyData)
}

Regards, Jim
 
Back
Top