Arif YILMAZ
Arif YILMAZ

Reputation: 5866

how to add a custom summaryitem to devexpress gridcontrol

I have a summary field in footer of a gridcontrol. In the gridcontrol, I have CheckButtons on the first column for users to select the records to process on. I need to fix summary field only to sum selected rows. Now it sums every row. How can I get it sum only the selected rows?

enter image description here

Upvotes: 2

Views: 9117

Answers (1)

nempoBu4
nempoBu4

Reputation: 6621

You need to change GridColumn.SummaryItem.SummaryType property to SummaryItemType.Custom and use GridView.CustomSummaryCalculate event to set the value of summary. But you cannot obtain information about selected rows in GridView.CustomSummaryCalculate event. That's why you need to calculate your sum in GridView.SelectionChanged event and use this sum in GridView.CustomSummaryCalculate event.
Here is example:

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;
}

Upvotes: 3

Related Questions