tags:

views:

515

answers:

2

I'm using vb.net 2008 and DataGridView. I'm looking for code that will allow me to have the Enter key move to the next column to the right instead of moving down one row while remaining in the same column.

A: 

Here's a CodeProject article that shows what you want to do:

Cheating the DataGridView

It's C# but it's also pretty simple.

The approached discussed in the article is to subclass the DataGridView to override the ProcessDialogKey event to handle the logic of selecting the next cell in the same row, or wrapping to first the column on the next row.

Most of the approaches for doing what you want to do seem to involve subclassing DataGridView.

Jay Riggs
+1  A: 

If you are confirming an edit, just move to the next column in the event CellEndEdit. If you are not in edit mode, you need to override ProcessDialogKey. See example below:

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);
}

}

0A0D
This solved the problem just great for me.
Nathan Palmer