Winform datagridview 控制滚轮事件
/// <summary> /// 给datagridview添加鼠标滚轮事件 /// </summary> /// <param name="dataGridView1"></param> public static void bindMouseWheel(System.Windows.Forms.DataGridView dataGridView1) { dataGridView1.MouseWheel += new System.Windows.Forms.MouseEventHandler(dataGridView1_MouseWheel); dataGridView1.TabIndex = 0; //获得焦点 } private static void dataGridView1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e) { DataGridView dataGridView1 = sender as DataGridView; try { 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; } } } } catch { return; } }