如何获取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;
 }

 

这个便可获得单元格左上角的座标和单元格的尺寸。以上方法可以直接使用。

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