如何獲取DataGridView的單元格座標和尺寸

最近做了一個小項目,需要確當單元格的座標和尺寸,現在就將方法和大家分享一下:

 public void DataGridViewCellAxix(DataGridView dgv,out int x,out int y,out int width,out int hight)
{          
            x = dgv.GetCellDisplayRectangle(dgv.CurrentCell.ColumnIndex, dgv.CurrentCell.RowIndex, false).Left + dgv.PointToScreen(dgv.Location).X;
            y = dgv.GetCellDisplayRectangle(dgv.CurrentCell.ColumnIndex, dgv.CurrentCell.RowIndex, false).Top + dgv.PointToScreen(dgv.Location).Y;
            width = dgv.GetCellDisplayRectangle(dgv.CurrentCell.ColumnIndex, dgv.CurrentCell.RowIndex, false).Width;
            hight = dgv.GetCellDisplayRectangle(dgv.CurrentCell.ColumnIndex, dgv.CurrentCell.RowIndex, true).Height;
 }

 

這個便可獲得單元格左上角的座標和單元格的尺寸。以上方法可以直接使用。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章