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

 

DataGridView上下方向键定位,布布扣,bubuko.com

DataGridView上下方向键定位

上一篇:自定义Mvc5 Owin 验证


下一篇:Android 消息分发 Handler 面试必须知道的