简体   繁体   中英

How to disable Tab key on particular column index of Datagridview?

Can Anybody please tell me how to disable Tab key only on a particular column index of datagridview ?

I have tried

if (e.KeyCode == Keys.Tab)
{
   e.SuppressKeyPress = true;
}

but it didn't worked.

i want something like

if(e.ColumnIndex==6)
{
   //disable tab key
}

Hook up KeyDown event of DataGridView and see if the selected cell belongs to column 6. If so eat the key.

void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Tab &&
        dataGridView1.SelectedCells
                     .Cast<DataGridViewCell>()
                     .Any(x => x.ColumnIndex == 6))
    {
        e.Handled = true;
    }
}

Now that's clear for me. KeyDown won't fire in editing mode because editing control will get those events. One way to do is override ProcessCmdKey in form or the usercontrol where DataGridView lives.

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData == Keys.Tab &&
        dataGridView1.EditingControl != null &&
        msg.HWnd == dataGridView1.EditingControl.Handle  &&
        dataGridView1.SelectedCells
            .Cast<DataGridViewCell>()
            .Any(x => x.ColumnIndex == 6))
    {
        return true;
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

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