gpt4 book ai didi

c# - 重复调用 CellValueNeeded

转载 作者:太空狗 更新时间:2023-10-29 23:28:35 25 4
gpt4 key购买 nike

我在表单上有一个垂直和水平可滚动的 DataGridView。

我使用虚拟模式是因为底层数据表很大。

当我向右滚动时,如果最后一列没有完全显示在 View 中,那么我会看到对 CellValueNeeded 的重复调用。

我该如何解决这个问题?

我的想法:

  1. 为什么 CellValueNeed 总是被重复调用以获取部分可见的列?也许我可以解决这个问题。

  2. 在 CelValueNeeded 中 - 我能否检测到它部分可见并在不处理的情况下返回?当我检查单元格值时,“显示”和“可见”均为真。

我的代码:

private void grid_Data_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
{
Console.WriteLine("CellValue: " + e.RowIndex + " " + e.ColumnIndex);
if (e.RowIndex > Grid.Rows.Count - 1)
return;
DataGridView gridView = sender as DataGridView;
e.Value = Grid.Rows[e.RowIndex][e.ColumnIndex];
gridView.Rows[e.RowIndex].HeaderCell.Value = (e.RowIndex).ToString();
}

EDIT1:

在 Digitalsa1nt 的回答后,我找到了解决问题的方法。这很复杂,因为第一列与最后一列的处理方式不同。如果您设置 RowHeaders,它会有所不同。

在上面的 CellValueNeed 中,如果以下函数为真,我现在返回。

    private bool IsPartiallyVisible(DataGridView gridView, DataGridViewCellValueEventArgs e)
{
if (gridView.FirstDisplayedScrollingColumnIndex == e.ColumnIndex)
{
if (gridView.FirstDisplayedScrollingColumnHiddenWidth != 0)
{
return true;
}
}

bool sameWidth = gridView.GetColumnDisplayRectangle(e.ColumnIndex, false).Width == gridView.GetColumnDisplayRectangle(e.ColumnIndex, true).Width;
return !sameWidth;
}

最佳答案

查看 MSDN documentation对于 CellValueNeeded 它读起来好像是一个标准的视觉事件,只要单元格变得“可见”就会触发,我认为它没有定义用于理解视觉偏向性的逻辑。看起来好像它试图为细胞完全“在视野中”做好准备。我怀疑没有暴露任何中间状态。

也就是说有一些建议here (SO 回复)和 here (奇怪的网络博客)提到使用 DataGridView.GetColumnDisplayRectangle 以确定单元格的矩形是否在屏幕边界内。

这是来自网络博客的片段:

The second parameter to GetColumnDisplayRectangle is called CutOverFlow, which is a Boolean value that controls whether the function returns the complete column rectangle (even if the column is not completely visible) or only the portion of the column's rectangle that is visible.

By calling this method twice, once with CutOverFlow set to true and once with it set to false, you can create a function that compares the results and returns a Boolean value when the column is only partially visible:

Return dg.GetColumnDisplayRectangle(columnindex, False).Width = _
dg.GetColumnDisplayRectangle(columnindex, True).Width

这将允许您在 grid_Data_CellValueNeeded 被调用时停止处理,并且以上根据最后的单元格位置返回 false。

关于c# - 重复调用 CellValueNeeded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50799100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com