c# - 如何向 devexpress gridcontrol 添加自定义摘要项

标签 c# winforms gridview devexpress

我在网格控件的页脚中有一个摘要字段。在网格控件中,我在第一列上有检查按钮,供用户选择要处理的记录。我需要修复汇总字段,仅对选定的行进行求和。现在它对每一行求和。我怎样才能只计算所选行的总和?

enter image description here

最佳答案

您需要更改GridColumn.SummaryItem.SummaryType属性至SummaryItemType.Custom并使用GridView.CustomSummaryCalculate事件来设置摘要的值。但是您无法在 GridView.CustomSummaryCalculate 事件中获取有关所选行的信息。这就是为什么您需要在 GridView.SelectionChanged 事件中计算总和,并在 GridView.CustomSummaryCalculate 事件中使用该总和。
这是示例:

private int _selectedSum;
private string _fieldName = "TOPLAM";

private void Form1_Load(object sender, EventArgs e)
{
    var column = gridView1.Columns[_fieldName];
    column.SummaryItem.SummaryType = SummaryItemType.Custom;
}

private void gridView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var column = gridView1.Columns[_fieldName];

    switch (e.Action)
    {
        case CollectionChangeAction.Add:
            _selectedSum += (int)gridView1.GetRowCellValue(e.ControllerRow, column);
            break;
        case CollectionChangeAction.Remove:
            _selectedSum -= (int)gridView1.GetRowCellValue(e.ControllerRow, column);
            break;
        case CollectionChangeAction.Refresh:

            _selectedSum = 0;

            foreach (var rowHandle in gridView1.GetSelectedRows())
                _selectedSum += (int)gridView1.GetRowCellValue(rowHandle, column);

            break;
    }

    gridView1.UpdateTotalSummary();
}

private void gridView1_CustomSummaryCalculate(object sender, CustomSummaryEventArgs e)
{
    var item = e.Item as GridColumnSummaryItem;

    if (item == null || item.FieldName != _fieldName)
        return;

    if (e.SummaryProcess == CustomSummaryProcess.Finalize)
        e.TotalValue = _selectedSum;
}

关于c# - 如何向 devexpress gridcontrol 添加自定义摘要项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25686372/

相关文章:

c# - 将变量从 Main 函数传递到另一个 C# 类

c# - 从C#中的异常“ArguementException未由用户代码处理”中获取更多详细信息

c# - 如何在 C# 和 WinForms 中将项目添加到列表框?

C# MySql.Data.MySqlClient.MySqlException (0x80004005) 问题

c# - 统一: Wrap mesh softly around other mesh?

c# - 在托管 DLL 中加载 C++ DLL 时出现 EEFileLoadException

c# - 从视频文件中提取wav文件

Android:两个不同 View 的同步滚动

android - 尝试在 gridview 中查看保存的相机图片时收到警告

android - 从 View 长按触发 DOWN 事件时从对话框注册 UP/CANCEL