我创建一个子类datagridview来覆盖鼠标轮事件以捕获鼠标滚动,然后将键向上或向下发送。我创建一个数据表,以绑定为mydatagridview的数据源,按一下按钮
private void button1_Click(object sender, EventArgs e)
{
DataTable myDataTable = new DataTable();
int NUM_ROWS = 150;
int NUM_COLS_TO_CREATE = 10;
for (int i = 0; i < NUM_COLS_TO_CREATE; i++)
{
myDataTable.Columns.Add("x" + i, typeof(string));
}
for (int i = 0; i < NUM_ROWS; i++)
{
var theRow = myDataTable.NewRow();
for (int j = 0; j < NUM_COLS_TO_CREATE; j++)
{
theRow[j] = "whatever";
}
//add the row *after* populating it
myDataTable.Rows.Add(theRow);
}
MyDataGridView1.DataSource = myDataTable;
}重写鼠标轮事件的代码如下
public partial class MyDataGridView : DataGridView
{
protected override void OnMouseWheel(MouseEventArgs e)
{
if (e.Delta < 0)
SendKeys.Send("{DOWN}");
else
SendKeys.Send("{UP}");
}
}如果我们使用鼠标轮缓慢地滚动每个项目,它的工作很好,但是如果使用鼠标轮滚动得太快,数据视图就会有点滞后。
例如,从第1行跳到第5行,它将从1跳到3,然后从3跳到5,这是另一个奇怪的问题。我每天都经常用"Navicat“。
所以如果我同时打开我的申请和Navicat。鼠标滚轮现在在我的应用程序上变得非常平滑,即使我滚动得太快。但是如果我关闭Navicat,滚动就会再次滞后。是什么导致了这一切?我很抱歉,如果我不能很好地解释它,我想要的只是想使滚动每一个项目顺利。有什么建议吗?
发布于 2018-04-21 08:40:36
正如@Bozhidar所提到的,我应该更好地处理MouseWheel事件,而不是重写它。所以我想出了解决办法,以防有人也需要。
在Form_Load中添加
MyDataGridView1.MouseWheel += new MouseEventHandler(MyDataGridView1_MouseWheel);然后把这个放在班上的任何地方。
private void MyDataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
HandledMouseEventArgs hme = (HandledMouseEventArgs)e;
hme.Handled = true;
int rowIndex = MyDataGridView1.CurrentCell.RowIndex;
int cellIndex = MyDataGridView1.CurrentCell.ColumnIndex;
MyDataGridView1.CurrentCell = MyDataGridView1.Rows[e.Delta < 0 ? Math.Min(rowIndex + 1, MyDataGridView1.RowCount - 1) : Math.Max(rowIndex - 1, 0)].Cells[cellIndex];
}发布于 2018-04-21 07:27:58
当涉及到精确的计时时,SendKeys方法就不那么可靠了--参见正式文件。尝试将"SendKeys“应用程序设置为"SendInput”,以强制新行为。
但是最好是处理-- MouseWheel事件,而不是重写它。您需要手动挂起它--不知道为什么它不存在于属性窗口中。考虑到您的DataGridView名为dgv
private void Form1_Load(object sender, EventArgs e)
{
dgv.MouseWheel += Dgv_MouseWheel;
}接下来,你考虑过FirstDisplayedScrollingRowIndex吗?只需在事件中适当设置它,并设置Handled标志,如下所示:
private void dgv_MouseWheel(object sender, MouseEventArgs e)
{
dgv.FirstDisplayedScrollingRowIndex += 3;
var he = (HandledMouseEventArgs);
he.Handled = true;
}发布于 2021-01-20 16:47:20
这里有一种方法可以保留滚动视图端口的默认滚动行为,但不更改行选择。
当在虚拟网格中处理数百万行时,它的执行速度也比内置鼠标轮处理程序快得多:
private void Form1_Load(object sender, EventArgs e)
{
DataGridView1.MouseWheel += DataGridView1_MouseWheel;
}
private void DataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
var hme = e as HandledMouseEventArgs;
hme.Handled = true;
int displayedRowIndex = DataGridView1.FirstDisplayedScrollingRowIndex;
// each "detente" scroll appears to create a delta of 120
// dividing delta by 120 to get the number of rows scrolled
// taking the negative of the delta so that it can be added to the displayedRowIndex intuitively as negative is down, positive is up
var rowDelta = -(e.Delta / 120);
var newDisplayedRowIndex = e.Delta < 0 ? Math.Min(displayedRowIndex + rowDelta, DataGridView1.RowCount - 1) : Math.Max(displayedRowIndex + rowDelta, 0);
DataGridView1.FirstDisplayedScrollingRowIndex = newDisplayedRowIndex;
}https://stackoverflow.com/questions/49953099
复制相似问题