DataGridView的,当我preSS输入进入到下一个单元格 [英] DataGridView-when I press enter it goes to the next cell

查看:108
本文介绍了DataGridView的,当我preSS输入进入到下一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5列,当我preSS确认则进入下一个单元格,当它到达行结束的时候我preSS进入它增加了一个新行一个DataGridView,但我的问题是,当我移动到previous行后,我preSS进入它跳跃的行,不进入下一个单元格,任何帮助?

 公共部分类Form1中:形态
{
    公共静态INT山口;
    公共静态INT行;

    公共Form1中()
    {
        的InitializeComponent();
    }

    私人无效Form1_Load的(对象发件人,EventArgs的)
    {
        dataGridView1.AllowUserToAddRows = FALSE;
        dataGridView1.Rows.Add();
    }

    私人无效dataGridView1_CellEnter(对象发件人,DataGridViewCellEventArgs E)
    {
        列= dataGridView1.CurrentCellAddress.X;
        行= dataGridView1.CurrentCellAddress.Y;
    }

    私人无效Form1_Key preSS(对象发件人,重点pressEventArgs E)
    {
        如果(e.KeyChar ==(INT)Keys.Enter)
        {
            如果(西+ 1小于5)
            {
                dataGridView1.CurrentCell = dataGridView1的[COL + 1,行]。
            }
            其他
            {
                dataGridView1.Rows.Add();
                dataGridView1.CurrentCell = dataGridView1的[COL  -  4,行+ 1];
            }
        }
    }
}
 

解决方案

忘记CellEnter事件和Form1_Key preSS事件也。刚刚处理的 dataGridView1_KeyDown 事件是这样的:

 私人无效dataGridView1_KeyDown(对象发件人,KeyEventArgs E)
    {
        如果(e.KeyData == Keys.Enter)
        {
            INT COL = dataGridView1.CurrentCell.ColumnIndex;
            INT行= dataGridView1.CurrentCell.RowIndex;

            如果(COL< dataGridView1.ColumnCount  -  1)
            {
                COL ++;
            }
            其他
            {
                COL = 0;
                排++;
            }

            如果(行== dataGridView1.RowCount)
                dataGridView1.Rows.Add();

            dataGridView1.CurrentCell = dataGridView1的[COL,排]。
            e.Handled =真实;
        }
    }
 

请注意,我改变了codeA一下,记得要设置处理的事件属性为true,否则会处理的默认行为。

干杯!

i have a datagridview with 5 columns,when i press "enter" it goes to the next cell and when it arrives to the end of the rows when i press enter it adds a new rows,but my problem is when i move to the previous rows after i press enter it jumps the rows and does not go to the next cells,any help?

public partial class Form1 : Form
{
    public static int Col;
    public static int Row;

    public Form1()
    {
        InitializeComponent();
    }       

    private void Form1_Load(object sender, EventArgs e)
    {
        dataGridView1.AllowUserToAddRows = false;
        dataGridView1.Rows.Add();           
    }   

    private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
    {           
        Col = dataGridView1.CurrentCellAddress.X;        
        Row = dataGridView1.CurrentCellAddress.Y;     
    }

    private void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {            
        if (e.KeyChar == (int)Keys.Enter)
        {              
            if (Col + 1 < 5)
            {
                dataGridView1.CurrentCell = dataGridView1[Col + 1, Row];
            }
            else
            {                        
                dataGridView1.Rows.Add();
                dataGridView1.CurrentCell = dataGridView1[Col - 4, Row + 1];
            }
        }
    }
}

解决方案

Forget about CellEnter event and the Form1_KeyPress event also. Just handle the dataGridView1_KeyDown event like this:

    private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Enter)
        {
            int col = dataGridView1.CurrentCell.ColumnIndex;
            int row = dataGridView1.CurrentCell.RowIndex;

            if (col < dataGridView1.ColumnCount - 1)
            {
                col ++;
            }
            else
            {
                col = 0;
                row++;
            }

            if (row == dataGridView1.RowCount)
                dataGridView1.Rows.Add();

            dataGridView1.CurrentCell = dataGridView1[col, row];
            e.Handled = true;
        }
    }

Please note that I changed the code a bit, and remember to set the Handled event property to true, otherwise it will process the default behavior.

Cheers!

这篇关于DataGridView的,当我preSS输入进入到下一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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