Home Forums WinForms controls Xceed Grid for WinForms Sum of values in group in title

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #12824 |

    I’m doing grouping in a grid on a certain column. In the title of the groups, I want to see the sum of the values of a column in that group.

    Something like this:

    ——————————————–
    |…Division.|.Name…..|.Amount………..|
    |——————————————–|
    |+ Division DIV_A : Total Amount = 9..|
    |……………………………………………….|
    |…DIV_A…| David…..|……………….3..|
    |…DIV_A…| Johnny…|……………….4..|
    |…DIV_A…| Willy……|………………..2..|
    |——————————————–|
    |+ Division DIV_B : Total Amount = 4..|
    |……………………………………………….|
    |…DIV_B…| Thomas..|……………….1..|
    |…DIV_B…| James….|……………….3..|
    ——————————————–

    Does anybody know how this could be done?

    Imported from legacy forums. Posted by Tommy (had 3015 views)

    User (Old forums)
    Member
    Post count: 23064

    This worked for me. It’s in C#. Hook into the GroupingUpdated event for the grid. This fires whenever the user modifies or creates a group. Inside the event, check each group’s datarows and add up the column. Simple. For a grid with lot’s of data, this might be slow. But as cumbersome as it looks, it’s actually quite zippy. Give it a try.

    grid.GroupingUpdated +=new EventHandler(grid_GroupingUpdated);
    private void grid_GroupingUpdated(object sender, EventArgs e)
    {
    foreach(Group grp in grid.Groups)
    {
    Xceed.Grid.Collections.ReadOnlyDataRowList rows = grp.GetSortedDataRows(false);
    int myTotal = 0;
    for (int i = 0; i < rows.Count; i++)
    {
    myTotal += Convert.ToInt32(rows[0].Cells[“<CellToSum>”].Value);
    }
    grp.Title = “Total Amount = ” + myTotal.ToString();
    }
    }

    Imported from legacy forums. Posted by MBonafe (had 549 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks a lot, it works like a charm. 😀

    Imported from legacy forums. Posted by Tommy (had 3888 views)

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.