【问题标题】:how to add a custom summaryitem to devexpress gridcontrol如何将自定义摘要项添加到 devexpress gridcontrol
【发布时间】:2014-10-30 10:28:25
【问题描述】:

我在网格控件的页脚中有一个汇总字段。在网格控件中,我在第一列上有 CheckButtons 供用户选择要处理的记录。我只需要修复汇总字段来汇总选定的行。现在它对每一行求和。我怎样才能得到它只对选定的行求和?

【问题讨论】:

  • 您的屏幕截图没有显示检查按钮。而且我不确定该汇总字段是网格视图控件的一部分还是您在表单上创建的内容。这可能很大程度上取决于您将哪种数据对象绑定到网格控件作为数据源。
  • @RenniePet,该汇总字段是网格视图的一部分,无论是否选中,它都会汇总所有内容
  • 好的,我已经使用了相当多的 XtraGrid,但从不使用汇总字段。 Soner Gönül 的链接是否为您解决了问题?如果没有,并且如果您希望我对此进行破解,请发布作为数据源对象的类的定义,即代表每一行以及集合对象(List 或其他) .

标签: c# winforms gridview devexpress


【解决方案1】:

您需要将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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多