在我的 C# (2010) 应用程序中,我有一个虚拟模式下的 DataGridView,它可以容纳数千行。是否可以找出当前屏幕上的单元格?
问问题
22427 次
3 回答
34
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...
}
}
}
}
更新:它现在可以找到可见的单元格。
于 2011-05-18T13:39:59.683 回答
1
我自己没有尝试过,但在我看来,使用DataGridView.GetRowDisplayRectangle确定一行的矩形并检查它是否与当前的DataGridView.DisplayRectangle重叠将是可行的方法。Rectangle.IntersectsWith可用于执行此操作。
作为一种优化,我会在找到第一个可见行之后使用DataGridView .DisplayedRowCount来确定哪些行是可见的。
于 2011-05-18T13:26:54.520 回答
1
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;
}
恕我直言
于 2020-02-18T09:05:49.563 回答