如何将自定义summaryitem添加到devexpress gridcontrol

时间:2014-09-05 12:51:46

标签: c# winforms gridview devexpress

我在gridcontrol的页脚中有一个摘要字段。在gridcontrol中,我在第一列上有CheckButtons,供用户选择要处理的记录。我只需要修改汇总字段来对所选行进行求和。现在它总结了每一行。我怎样才能将它与所选行相加?

enter image description here

1 个答案:

答案 0 :(得分:2)

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