如何找出当前屏幕上有哪些 DataGridView 行?

发布于 2024-11-08 16:20:05 字数 78 浏览 0 评论 0原文

在我的 C# (2010) 应用程序中,我有一个处于虚拟模式的 DataGridView,其中包含数千行。是否可以找出当前屏幕上有哪些单元格?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风吹雨成花 2024-11-15 16:20:05
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...
                }
            }
        }
    }

更新:它现在可以找到可见的单元格。

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.

三生一梦 2024-11-15 16:20:05

我自己没有尝试过,但在我看来,使用 DataGridView.GetRowDisplayRectangle 并检查它是否与当前 DataGridView.DisplayRectangle 将是正确的选择。 Rectangle.IntersectsWith 对此很有用。

作为优化,我将使用 DataGridView .DisplayedRowCount 找到第一个可见行后确定哪些行是可见的。

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.

爱人如己 2024-11-15 16:20:05
    private bool RowIsVisible(DataGridViewRow row)
    {
        DataGridView dgv = row.DataGridView;
        int firstVisibleRowIndex = dgv.FirstDisplayedCell.RowIndex;
        int lastVisibleRowIndex = firstVisibleRowIndex + dgv.DisplayedRowCount(false) - 1;
        return row.Index >= firstVisibleRowIndex && row.Index <= lastVisibleRowIndex;
    }

恕我直言
问候

    private bool RowIsVisible(DataGridViewRow row)
    {
        DataGridView dgv = row.DataGridView;
        int firstVisibleRowIndex = dgv.FirstDisplayedCell.RowIndex;
        int lastVisibleRowIndex = firstVisibleRowIndex + dgv.DisplayedRowCount(false) - 1;
        return row.Index >= firstVisibleRowIndex && row.Index <= lastVisibleRowIndex;
    }

IMHO
Regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文