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;
}

 

posted @ 2014-08-13 10:23  StarkSoft  阅读(1405)  评论(0编辑  收藏  举报