datagridview keypress事件添加新行 [英] datagridview keypress event for adding new row

查看:157
本文介绍了datagridview keypress事件添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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 。加();
}
}
}

谢谢你的时间,对不起关于我的英语无论如何

解决方案

这将添加一个如果当前单元格是 DGV 中的最后一个单元格,并且用户按 Tab



(注意(显然)用户现在不能从 DGV 中选出,除非是 backtabbing 超过第一个单元格..)

  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();
//我们可以返回true;这里要禁止键
//但是我们真的想进入新行..!
}

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

任何尝试使用 DGV 将最终离开 DGV 而不是添加 ..


i have a datagridview that has 6 column, i want to add a new row every time i press "Tab" button (only) on the last cell of the column, i used the code bellow to prevent adding row everytime i write cell value

dataGridView1.AllowUserToAddRows = false;
dataGridView1.Rows.Add();

i have already use keypress event on cell[5] (last cell) but it does not work, last cell was set to read only

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

thanks for your time, sorry about my english anyway

解决方案

This will add a Row if and only if the current cell is the last one in the DGV and the user presses Tab.

(Note that (obviously) the user now can't tab out of the DGV, except by backtabbing over the first cell..)

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

Any attempt to use any of the Key events of the DGV will eventually leave the DGV instead of adding a Row..

这篇关于datagridview keypress事件添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆