首页 > 代码库 > DataGridView上下方向键定位

DataGridView上下方向键定位

/// <summary>/// DataGridView上下方向键定位/// </summary>/// <param name="dgv"></param>/// <param name="e"></param>/// <returns></returns>public static DataGridViewCell DataGridView_KeyUpDownScrollToRow(DataGridView dgv, KeyEventArgs e){if (e.KeyCode == Keys.Up)//向上方向键{DataGridViewRow dgvCurrentRow = dgv.CurrentRow;int currentRow = 0;if (dgvCurrentRow != null){if (dgvCurrentRow.Index == 0){currentRow = dgv.RowCount - 1;}else{currentRow = dgvCurrentRow.Index - 1;}}return dgv.Rows[currentRow].Cells[0];}if (e.KeyCode == Keys.Down)//向下方向键{DataGridViewRow dgvCurrentRow = dgv.CurrentRow;int currentRow = 0;if (dgvCurrentRow != null){if (dgvCurrentRow.Index == dgv.RowCount - 1){currentRow = 0;}else{currentRow = dgvCurrentRow.Index + 1;}}return dgv.Rows[currentRow].Cells[0];}return null;}