/// <summary> 3 /// 给datagridview添加鼠标滚轮事件 4 /// </summary> 5 /// <param name="dataGridView1"></param> 6 public static void bindMouseWheel(System.Windows.Forms.DataGridView dataGridView1) 7 { 8 dataGridView1.MouseWheel += new System.Windows.Forms.MouseEventHandler(dataGridView1_MouseWheel); 9 dataGridView1.TabIndex = 0;//获得焦点 10 } 11 12 static void dataGridView1_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e) 13 { 14 DataGridView dataGridView1 = sender as DataGridView; 15 try 16 { 17 if (dataGridView1.CurrentCell != null) 18 { 19 DataGridViewCell dvc = dataGridView1.CurrentCell; 20 int ri = dvc.RowIndex; 21 int ci = dvc.ColumnIndex; 22 if (e.Delta > 0)//向上 23 { 24 if (ri > 0) 25 { 26 dvc = dataGridView1.Rows[ri - 1].Cells[ci]; 27 dataGridView1.CurrentCell = dvc; 28 } 29 } 30 else 31 { 32 if (ri < dataGridView1.Rows.Count - 1) 33 { 34 dvc = dataGridView1.Rows[ri + 1].Cells[ci]; 35 dataGridView1.CurrentCell = dvc; 36 } 37 } 38 } 39 } 40 catch 41 { 42 return; 43 } 44 }
参考:http://archive.cnblogs.com/a/1768664/