gpt4 book ai didi

c# - 在 Devexpress XtraGrid GroupRow 中显示递归行数

转载 作者:太空宇宙 更新时间:2023-11-03 23:06:35 25 4
gpt4 key购买 nike

我有一个包含多个组的 gridview,我使用 CustomDrawGroupRow 事件来显示每个组的行数:

private void gridView_CustomDrawGroupRow(object sender, DevExpress.XtraGrid.Views.Base.RowObjectCustomDrawEventArgs e)
{
var view = (GridView)sender;
var info = (GridGroupRowInfo)e.Info;
var caption = info.Column.Caption;
if (info.Column.Caption == string.Empty)
{
caption = info.Column.ToString();
}
info.GroupText = $"{caption} : {info.GroupValueText} ({view.GetChildRowCount(e.RowHandle)})";
}

Grid View

现在我想递归地显示行数,以便第一级显示 2171 (1913 + 135 + 123) 的计数。

这是我尝试过的方法,但它抛出一个 StackOverflowException 并且我看不到这里的问题:

private void gridView_CustomDrawGroupRow(object sender, DevExpress.XtraGrid.Views.Base.RowObjectCustomDrawEventArgs e)
{
var view = (GridView)sender;
var info = (GridGroupRowInfo)e.Info;
var caption = info.Column.Caption;
if (info.Column.Caption == string.Empty)
{
caption = info.Column.ToString();
}
info.GroupText = $"{caption} : {info.GroupValueText} ({GetRowCountRecursive(view, e.RowHandle)})";
}

private int GetRowCountRecursive(GridView view, int rowHandle)
{
int totalCount = 0;
int childrenCount = view.GetChildRowCount(rowHandle);
for (int i = 0; i < childrenCount; i++)
{
var childRowHandle = view.GetChildRowHandle(rowHandle, i);
totalCount += GetRowCountRecursive(view, childRowHandle);
}
return totalCount;
}

最佳答案

我没有检查 childRowHandle 是否是一个包含 IsGroupRow() 的组行。如果不是,递归必须停止并且 totalCount 需要增加 1。

private int GetRowCountRecursive(GridView view, int rowHandle)
{
int totalCount = 0;
int childrenCount = view.GetChildRowCount(rowHandle);
for (int i = 0; i < childrenCount; i++)
{
var childRowHandle = view.GetChildRowHandle(rowHandle, i);
if (view.IsGroupRow(childRowHandle))
{
totalCount += GetRowCountRecursive(view, childRowHandle);
}
else
{
totalCount++;
}
}
return totalCount;
}

Grid View

关于c# - 在 Devexpress XtraGrid GroupRow 中显示递归行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41019188/

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