简体   繁体   English

在Datagridview中输入按键选择进入下一行,但我要进入下一列?

[英]In Datagridview enter key press selection goes to next row but i want goes to next column?

In DataGridView enter key press selection goes to next row but I want goes to next column? DataGridView输入按键选择会转到下一行,但是我要转到下一列? How can fix it? 如何解决?

private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{       
   if (e.KeyCode == Keys.Enter)
   {
   }
}

There is a property named CurrentCell in the class DataGridView , so you should use it: 在类DataGridView有一个名为CurrentCell的属性,因此您应该使用它:

    private void dataGridView1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            int newRow;
            int newColumn;
            if (dataGridView1.CurrentCell.ColumnIndex == dataGridView1.ColumnCount-1)         // it's a last column, move to next row;
            {
                newRow = dataGridView1.CurrentCell.RowIndex + 1;
                newColumn = 0;

                if (newRow == dataGridView1.RowCount)
                    return; // ADD new row or RETURN (depends of your purposes..)
            }
            else                // just change current column. row is same
            {
                newRow = dataGridView1.CurrentCell.RowIndex;
                newColumn = dataGridView1.CurrentCell.ColumnIndex + 1;
            }

            dataGridView1.CurrentCell = dataGridView1.Rows[newRow].Cells[newColumn];
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM