Skip to main content Skip to footer

How to Hide Empty Groups with Subtotal

The Subtotal method adds rows that contain aggregate data for the regular (data) rows. When the Subtotal method adds rows with the aggregate information, it automatically assigns subtotal styles to the new rows (there are built-in styles for 5 levels of subtotals). By design, C1FlexGrid does not automatically hide groups with no visible children when filtering is applied on a grid containing Subtotals. This code example shows how one can customize this behavior such that the Node rows with empty children are not displayed after filtering is applied. To implement this we'll apply custom filtering in the grid and handle the behavior manually.

Creating Subtotals in the grid

c1FlexGrid1.Subtotal(AggregateEnum.Clear);  
c1FlexGrid1.Subtotal(AggregateEnum.None, 0, 1, 1, "{0}");  
c1FlexGrid1.Subtotal(AggregateEnum.None, 1, 2, 2, "{0}");

Apply Custom Filtering


private void c1FlexGrid1_AfterFilter(object sender, EventArgs e)  
{  
    // suspend repainting for efficiency  
    c1FlexGrid1.BeginUpdate();  

    // update totals  
    c1FlexGrid1.Subtotal(AggregateEnum.Clear);  
    c1FlexGrid1.Subtotal(AggregateEnum.None, 0, 1, 1, "{0}");  
    c1FlexGrid1.Subtotal(AggregateEnum.None, 1, 2, 2, "{0}");  

    // hide totals with no visible children  
    int i = 0;  

    for (i = 0; i <= c1FlexGrid1.Rows.Count - 1; i++)  
    {  
        Row r = c1FlexGrid1.Rows[ i ] as Row;  

        if (r.IsNode)  
        {  
            // check if this node has visible children  
            bool hasVisibleChilren = false;  
            CellRange rng = r.Node.GetCellRange();  
            int j = 0;  
            for (j = rng.TopRow; j <= rng.BottomRow; j++)  
            {  
                Row childRow = c1FlexGrid1.Rows[j];  
                if (childRow.IsVisible & !childRow.IsNode)  
                {  
                    hasVisibleChilren = true;  
                    break;  
                }  
            }  

            // show node iff it has visible children  
            r.Visible = hasVisibleChilren;  

        }  
    }  

    // all done  
    c1FlexGrid1.EndUpdate();  
}