📜  允许使用滚轮鼠标滚动datagridview c#代码示例

📅  最后修改于: 2022-03-11 14:48:49.348000             🧑  作者: Mango

代码示例1
private void DataGridView1_MouseEnter(object sender, EventArgs e)
        {
            DataGridView1.Focus();
        }

then Add Mouse wheel event in Load function 
DataGridView1.MouseWheel += new MouseEventHandler(DataGridView1_MouseWheel);

Finally Create Mouse wheel function

void DataGridView1_MouseWheel(object sender, MouseEventArgs e)
{
    int currentIndex = this.DataGridView1.FirstDisplayedScrollingRowIndex;
    int scrollLines = SystemInformation.MouseWheelScrollLines;

    if (e.Delta > 0) 
    {
        this.DataGridView1.FirstDisplayedScrollingRowIndex = Math.Max(0, currentIndex - scrollLines);
    }
    else if (e.Delta < 0)
    {
        if (this.DataGridView1.Rows.Count > (currentIndex + scrollLines))
            this.DataGridView1.FirstDisplayedScrollingRowIndex = currentIndex + scrollLines;
    }
}