Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用于添加新行的datagridview按键事件_C#_Datagridview_Row_Keypress - Fatal编程技术网

C# 用于添加新行的datagridview按键事件

C# 用于添加新行的datagridview按键事件,c#,datagridview,row,keypress,C#,Datagridview,Row,Keypress,我有一个datagridview,它有6列,我想在每次按下列的最后一个单元格上的“Tab”按钮(仅)时添加一个新行,我使用下面的代码来防止每次写入单元格值时添加行 dataGridView1.AllowUserToAddRows = false; dataGridView1.Rows.Add(); 我已经在单元格[5](最后一个单元格)上使用了按键事件,但它不起作用, 最后一个单元格设置为只读 private void dataGridView1_KeyPress(object sender,

我有一个datagridview,它有6列,我想在每次按下列的最后一个单元格上的“Tab”按钮(仅)时添加一个新行,我使用下面的代码来防止每次写入单元格值时添加行

dataGridView1.AllowUserToAddRows = false;
dataGridView1.Rows.Add();
我已经在单元格[5](最后一个单元格)上使用了按键事件,但它不起作用, 最后一个单元格设置为只读

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (dataGridView1.CurrentCell.ColumnIndex == 5)
    {
        if (e.KeyChar == (char)Keys.Tab)
        {
            dataGridView1.Rows.Add();
        }
    }
}

感谢您的时间,对我的英语感到抱歉。

如果且仅当当前单元格是
DGV
中的最后一个单元格,并且用户按下
选项卡时,这将添加一行

(请注意(显然)用户现在无法从
DGV
中进行制表,除非通过在第一个单元格上反向制表。


任何使用
DGV
的任何关键事件的尝试最终都将离开
DGV
,而不是添加

尝试将其更改为:dataGridView1.Rows.Add(new DataGridViewRow());“上次单元格设置为只读”是什么意思?请参阅我编辑的答案!
int yourLastColumnIndex = dataGridView.Columns.Count - 1;

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (dataGridView.Focused && keyData == Keys.Tab) &&
        if (dataGridView.CurrentCell.ColumnIndex == yourLastColumnIndex
            dataGridView.CurrentRow.Index == dataGridView.RowCount - 1)
        {
            dataGridView.Rows.Add();
            // we could return true; here to suppress the key
            // but we really want to move on into the new row..!
        }

    return base.ProcessCmdKey(ref msg, keyData);
}