如何做到鼠标滚动时,让dataGridView1内的记录焦点也上下移动?
只是行的焦点移动,非记录移动!!! 如果行的焦点不能向下移动了,鼠标滚动也不生效;
鼠标单击或双击时,不可以有让dataGridView1.Rows[i].ReadOnly = true的行获得焦点?直接返回到原来的焦点单元格。
只是行的焦点移动,非记录移动!!! 如果行的焦点不能向下移动了,鼠标滚动也不生效;
鼠标单击或双击时,不可以有让dataGridView1.Rows[i].ReadOnly = true的行获得焦点?直接返回到原来的焦点单元格。
只是行的焦点移动,非记录移动!!! 如果行的焦点不能向下移动了,鼠标滚动也不生效; this.dataGridView1.MultiSelect = false;
this.dataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; this.dataGridView1.MouseWheel += new MouseEventHandler(dataGridView1_MouseWheel);
void dataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
int i = e.Delta / -120;
if (this.dataGridView1.CurrentRow.Index + i > this.dataGridView1.Rows.Count - 1)
{
this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Selected = true;
}
else if (this.dataGridView1.CurrentRow.Index + i < 0)
{
this.dataGridView1.Rows[0].Selected = true;
}
else
{
this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex + i].Selected = true;
}
this.dataGridView1.CurrentCell = this.dataGridView1.SelectedRows[0].Cells[0];
}
void dataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
int i = e.Delta / -120;
if (this.dataGridView1.CurrentRow.Index + i > this.dataGridView1.Rows.Count - 1)
{
this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Cells[this.dataGridView1.CurrentCell.ColumnIndex].Selected = true;
}
else if (this.dataGridView1.CurrentRow.Index + i < 0)
{
this.dataGridView1.Rows[0].Cells[this.dataGridView1.CurrentCell.ColumnIndex].Selected = true;
}
else
{
this.dataGridView1.Rows[this.dataGridView1.CurrentCell.RowIndex + i].Cells[this.dataGridView1.CurrentCell.ColumnIndex].Selected = true;
}
this.dataGridView1.CurrentCell = this.dataGridView1.SelectedCells[0];
}
{
DataGridView dataGridView1 = sender as DataGridView;
if(dataGridView1.CurrentCell != null)
{
DataGridViewCell dvc = dataGridView1.CurrentCell;
int ri = dvc.RowIndex;
int ci = dvc.ColumnIndex;
if (e.Delta > 0)//向上
{
if (ri > 0)
{
dvc = dataGridView1.Rows[ri - 1].Cells[ci];
dataGridView1.CurrentCell = dvc;
}
}
else
{
if(ri < dataGridView1.Rows.Count - 1)
{
dvc = dataGridView1.Rows[ri + 1].Cells[ci];
dataGridView1.CurrentCell = dvc;
}
}
}
}