简体   繁体   中英

how to control datagridview cursor movement in C#

I'd like my datagridview cursor to move right to the next column instead of moving to the next row after entering data to a cell.

I've attempted to take control of the cursor by capturing the keys via dataGridView1_KeyDown event but this does not prevent the cursor from moving to the next row after entering data to a cell ...

Thanks in advance for your assistance.

Cheers,

Here are an answer from Mark Rideout (DatagridView Program Manager)

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=157055&SiteID=1

Scroll down 4 posts (and then more because they make better versions later on in the thread).

For future references if link will expire ( All credits to Mark Rideout ):

Create a new class named dvg that inherits from DataGridView. Compile project and then use this extended Datagridview-control instead of the normal and you'll have a datagridview that selects the next cell when pressing enter:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM