c# datagridview control

本文关键字:control datagridview | 更新日期: 2023-09-27 18:08:47

我在双击后为datagridview添加控件(粗略代码):

DoubleClick
{  
    datagridview.Controls.Add(newControl);
    datagridview.ClearSelection();  //to be shure that any of cells are selected
    newControl.Focus();
    newControl.KeyUp += new KeyEventHandler(newControl_KeyUp);
}

:

newControl_KeyUp(object sender, KeyEventArgs e)
{
    if(e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab || e.KeyCode == Keys.A)
    {
        Commit();
    }
}

我的新控件对键起作用。一个。但它对tab键和回车键不起作用。当我单击enter或tab时,只有datagridview所选单元格更改。

c# datagridview control

由于Tab键使焦点移动,如果可以的话,我建议您在较低的级别上工作:

private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
    Control newControl = new MyTextBox();
    dataGridView1.Controls.Add(newControl);
    dataGridView1.ClearSelection();  //to be sure that any of cells are selected  
    newControl.Focus();
}
class MyTextBox : TextBox
{
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == Keys.Enter || keyData == Keys.Tab || keyData == Keys.A)
        {
            Trace.WriteLine("Ok, key = " + keyData);
            return true;///Or false??? return to override the basic behavior
        } 
        return base.ProcessCmdKey(ref msg, keyData);
    }
 }