先来看个例子
DataGridView上下方向键定位
代码如下///
/// DataGridView上下方向键定位
///
///
///
///
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;
}
光标定位最后行的方法
定位行:
代码如下1.BindingSource.MoveLast();
2.dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows. Count-1;
定位单元格:
代码如下1.dataGridView1.FirstDisplayedCell = dataGridView1.Rows[xxx].Cells[0];
2.dataGridView1.CurrentCell = dataGridView1.Rows[dataGridView1.Rows.Count-1].Cells[0];
dataGridView1.BeginEdit(true);
DataGridView 定位行的问题
添加,删除,查找之后希望 Grid定位到需要的行
代码如下Me.dgvPaper.Rows(i).Selected = True 设置能保证选中行,
一旦找到了就定位到相应的位置,如果数据行很多的话,希望滚动条滚到相应的位置。
DataGridView控件有一个FirstDisplayedScrollingRowIndex属性,把需要定位的行index(N)赋值给这个属性之后,DataGridView的显示矩形区域内的第一行就是你说指定的行号了
代码如下Me.dgvPaper.FirstDisplayedScrollingRowIndex = i
有发现一个问题,
这样设置之后,取 CurrentRows 时候会出现和 Selected rows不同的行,
这样设置就没问题了
并且滚动条也会自动的滚动,显示选中的行
省去了
"Me.dgvThing.Rows(i).Selected = True
"Me.dgvThing.FirstDisplayedScrollingRowIndex = i
asp.net中datagridview|Asp.net中DataGridView定位与定位问题解决
http://m.bbyears.com/jiaocheng/56279.html
推荐访问: