DataGridView通過滑鼠座標,獲取所在行索引

世紀緣發表於2016-10-18

DataGridView.HitTest 方法 (Int32, Int32)

在給定了 x 座標和 y 座標的情況下返回位置資訊,例如,行索引和列索引。


   private DataGridViewCell clickedCell;

   private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
   {
// If the user right-clicks a cell, store it for use by the shortcut menu.
       if (e.Button == MouseButtons.Right)
       {
           DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);
           if (hit.Type == DataGridViewHitTestType.Cell)
           {
               clickedCell =
                   dataGridView1.Rows[hit.RowIndex].Cells[hit.ColumnIndex];
           }
       }
   }

相關文章