[英]In Datagridview enter key press selection goes to next row but i want goes to next column?
在DataGridView
輸入按鍵選擇會轉到下一行,但是我要轉到下一列? 如何解決?
private void textBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
}
}
在類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.