How to find out which DataGridView rows are currently onscreen?

被刻印的时光 ゝ 提交于 2019-11-29 12:30:55

问题


In my C# (2010) application I have a DataGridView in Virtual Mode which holds several thousand rows. Is it possible to find out which cells are onscreen at the moment?


回答1:


public void GetVisibleCells(DataGridView dgv)
    {
        var visibleRowsCount = dgv.DisplayedRowCount(true);
        var firstDisplayedRowIndex = dgv.FirstDisplayedCell.RowIndex;
        var lastvisibleRowIndex = (firstDisplayedRowIndex + visibleRowsCount) - 1;
        for (int rowIndex = firstDisplayedRowIndex; rowIndex <= lastvisibleRowIndex; rowIndex++)
        {
            var cells = dgv.Rows[rowIndex].Cells;
            foreach (DataGridViewCell cell in cells)
            {
                if (cell.Displayed)
                {
                    // This cell is visible...
                    // Your code goes here...
                }
            }
        }
    }

Updated: It now finds visible cells.




回答2:


I haven't tried this myself, but it seems to me that determining the rectangle of a row using DataGridView.GetRowDisplayRectangle and checking if it overlaps the current DataGridView.DisplayRectangle would be the way to go. Rectangle.IntersectsWith is useful in to do this.

As an optimization I would use DataGridView .DisplayedRowCount after finding the first visible row to determine what rows are visible.



来源:https://stackoverflow.com/questions/6045161/how-to-find-out-which-datagridview-rows-are-currently-onscreen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!