public void Form5_MouseWheel(object sender, MouseEventArgs e)
{
int index=dataGridView1.SelectedRows[0].Index;
System.Drawing.Point p = PointToScreen(e.Location);
if (WindowFromPoint(p.X, p.Y) == dataGridView1.Handle.ToInt32())
{
if (e.Delta == 120)
{
if (index >= 1)
{
dataGridView1.Rows[index - 1].Selected = true;
}
}
else if (e.Delta < 0)
{
dataGridView1.Rows[index + 1].Selected = true;
}
dataGridView1.Focus();
}
} private void dataGridView1_MouseEnter(object sender, EventArgs e)
{
this.MouseWheel += new MouseEventHandler(Form5_MouseWheel);
}
我捣鼓了一下,这个实现了滚动滑轮记录跟着移动,但是移到dataGridView1显示的范围边缘下面就看不见了
你看看有什么办法解决
public void Form5_MouseWheel(object sender, MouseEventArgs e)
{
int index=dataGridView1.SelectedRows[0].Index;
System.Drawing.Point p = PointToScreen(e.Location);
if (WindowFromPoint(p.X, p.Y) == dataGridView1.Handle.ToInt32())
{
if (e.Delta == 120)
{
if (index >= 1)
{
dataGridView1.Rows[index - 1].Selected = true;
}
}
else if (e.Delta < 0)
{
dataGridView1.Rows[index + 1].Selected = true;
}
dataGridView1.Focus();
this.dataGridView1.FirstDisplayedScrollingRowIndex = index;//始终显示选中的行
}
} private void dataGridView1_MouseEnter(object sender, EventArgs e)
{
this.MouseWheel += new MouseEventHandler(Form5_MouseWheel);
}
搞定了 Lz试试吧
//最上面忘了两行
[DllImport("user32.dll")]
public static extern int WindowFromPoint(int xPoint, int yPoint);
//Form.designer.cs里面在datagridview的地方加入鼠标滚轮事件
this.dataGridView1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.dataGridView1_MouseWheel); private void dataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
int rowIndex = this.dataGridView1.CurrentRow.Index;
this.dataGridView1.ClearSelection(); if (e.Delta > 0)
{
if (rowIndex > 0)
{
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[rowIndex - 1].Cells[0];
this.dataGridView1.Rows[rowIndex - 1].Selected = true;
}
else
{
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[rowIndex].Cells[0];
this.dataGridView1.Rows[rowIndex].Selected = true;
}
}
else
{
if (rowIndex < this.dataGridView1.Rows.Count - 1)
{
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[rowIndex + 1].Cells[0];
this.dataGridView1.Rows[rowIndex + 1].Selected = true;
}
else
{
this.dataGridView1.CurrentCell = this.dataGridView1.Rows[rowIndex].Cells[0];
this.dataGridView1.Rows[rowIndex].Selected = true;
}
}
}
datagridview里也有的
刚试了下上面的代码,都不能通过,不知道是不是我弄错了什么,两位牛人多指点些我容易忽略的地方,我好多东西不懂的
this.dataGridView1.FirstDisplayedScrollingRowIndex = index;//始终显示选中的行
this.dataGridView1.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.dataGridView1_MouseWheel);这个事件一定要加入,这个事件是在datagridview得到焦点以后,鼠标的滚轮在上面来回滚触发的
在Form.designer.cs里面在datagridview的地方加入鼠标滚轮事件了,加了后就显示有错误啊
dataGridView1_MouseWheel下有红划线啊
to 14楼
我是新手,你的那个win32 API调用我都没用过
详细讲下,是要加那个什么 using <mscorlib.dll> 吧,我很不懂
[DllImport("user32.dll")]改成
[System.Runtime.InteropServices.DllImport("user32.dll")]
我在属性栏里就没找到SelectionMode属性和MouseWheel属性
---》
SelectionMode是datagridview的属性
MouseWheel是datagridview的事件