Movement to rows in a DataGrid

F

Faith

Hi there.
I need the TAB key to work like the ENTER key in a Data
Grid. Basically I have several columns in a Data Grid and
only the first column is editable (or not Read Only).
When the user types in a number (in column 0) and presses
the TAB key it goes into the Read Only (non-editable)
column (column 1), I need it to go to the next Row and
stay in the first column (column 0). Can someone tell me
how to do this and if it's possible? Or is there any
reference web site out there you can point me to?

Thanks so much!
Faith
 
T

Tian Min Huang

Hi Faith,

Thanks for your post. We can intercept TAB key in DataGrid control, and
then set the focus to corresponding Cell. Here is the detailed steps:

1. We must create a DataGrid-derived class in order to trap keytrokes in
the DataGrid. Please refer to the following code snippet:

//------------------code snippet------------------
public class MyDataGrid: DataGrid
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
const int WM_KEYDOWN = 0x100;
const int WM_SYSKEYDOWN = 0x104;

if ((msg.Msg == WM_KEYDOWN) || (msg.Msg == WM_SYSKEYDOWN))
{
switch(keyData)
{
case Keys.Tab:
this.CurrentCell = new
DataGridCell(this.CurrentCell.RowNumber+1,0);
return true;
}
}
return base.ProcessCmdKey(ref msg,keyData);
}
}
//------------------end of-------------------

2. In the Form class, declare and create the DataGrid as MyDataGrid.

CHANGE:
private System.Windows.Forms.DataGrid dataGrid1;
this.dataGrid1 = new System.Windows.Forms.DataGrid();

TO:
private MyDataGrid dataGrid1;
this.dataGrid1 = new MyDataGrid();

Please refer to the following KB article for detailed information:
HOW TO: Trap Keystrokes in .NET Controls by Using Visual C# .NET
http://support.microsoft.com/?id=320584

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top