简体   繁体   中英

How to move cursor to the next row in datagridview

I have a Windows Form that contains 2 datagridviews and a button between them to transfer the data. I cannot figure out how should i move the cursor of selected row to the next row after the button has successfully transfer the data.

if (dataGridView1.CurrentRow != null)
    dataGridView1.CurrentCell =
        dataGridView1
        .Rows[Math.Min(dataGridView1.CurrentRow.Index + 1, dataGridView1.Rows.Count - 1)]
        .Cells[dataGridView1.CurrentCell.ColumnIndex];
if (e.KeyChar == (char)13)
{
    int col = grdIdeal.CurrentCell.ColumnIndex;
    int row = grdIdeal.CurrentCell.RowIndex;
    int nRows = grdIdeal.Rows.Count - 1;
    int nCol = grdIdeal.Columns.Count - 1;

    if (nCol == col && nRows == row)
        grdIdeal.CurrentCell = grdIdeal[0, 0];
    else if (nRows == row)
        grdIdeal.CurrentCell = grdIdeal[col + 1, 0];
    else
        grdIdeal.CurrentCell = grdIdeal[col, row + 1];
    e.Handled = true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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