DataGridView GetCellDisplayRectangle太慢了

时间:2014-03-04 13:10:47

标签: c# datagridview

我正在实现功能,它在DGV中的选定单元格周围绘制矩形(就像在MS Excel中一样)。此功能在每个SelectionChanged事件中发生,并从SelectedCells中找到两个对角线单元格,并在它们之间绘制Rectangle

然而方法GetCellDisplayRectangle(index,index,bool);实际上很慢,需要 ~1300ticks ,而且我会调用它两次。结果是选择矩形渲染太慢而且看起来不平滑。

所以我的问题是,有没有其他更快的方法如何在DGV中找到特定细胞的X,Y,高度,宽度?

谢谢 WH

2 个答案:

答案 0 :(得分:2)

private Rectangle GetCellRectangle(int columnIndex, int rowIndex)
        {
            Rectangle selRect = new Rectangle(0, 0, 0, 0);

            selRect.X = RowHeadersWidth + 1;
            for (int i = FirstDisplayedScrollingColumnIndex; i < columnIndex; i++)
            {
                selRect.X += Columns[i].Width;
            }
            selRect.X -= FirstDisplayedScrollingColumnHiddenWidth;

            selRect.Y = ColumnHeadersHeight + 1;
            for (int i = FirstDisplayedScrollingRowIndex; i < rowIndex; i++)
            {
                selRect.Y += Rows[i].Height;
            }

            selRect.Width = Rows[rowIndex].Cells[columnIndex].Size.Width;
            selRect.Height = Rows[rowIndex].Cells[columnIndex].Size.Height;
            return selRect;
        }

此方法返回Cell Display Rectangle大约8-10个刻度,而不是1300个。

答案 1 :(得分:0)

您可以使用

获取单元格的高度和宽度
cell.ContentBound.Width /*and */ cell.ContentBound.Height

这些返回int中的宽度和高度。

否则就有Size属性

cell.Size.Width /*and */ cell.Size.Height