This is a migrated thread and some comments may be shown as answers.

Group footer label?

3 Answers 167 Views
GridView
This is a migrated thread and some comments may be shown as answers.
MC
Top achievements
Rank 1
MC asked on 01 Dec 2016, 05:02 AM

How can I insert text in a group footer's cell?

 

01.public MainWindow()
02.{
03.    InitializeComponent();
04. 
05.    var style = new Style();
06.    style.Setters.Add(new Setter(GroupHeaderRow.ShowGroupHeaderColumnAggregatesProperty, false));
07.    style.Setters.Add(new Setter(GroupHeaderRow.ShowHeaderAggregatesProperty, false));
08.    var radGrid = new RadGridView { GroupRenderMode = GroupRenderMode.Flat, GroupRowStyle = style};
09.    Content = radGrid;
10. 
11.    // Table schema
12.    var data = new DataTable();
13.    var user = data.Columns.Add("User");
14.    var cat = data.Columns.Add("Category");
15.    var amt = data.Columns.Add("Amount", typeof(decimal));
16. 
17.    // Populate values
18.    var catValues = new[] {"Cat A", "Cat B", "Cat C"};
19.    for (var i = 0; i < 20; i++)
20.    { data.Rows.Add("User " + i, catValues[i % catValues.Length], i * 100d); }
21.             
22.    // Prepare RadGridView
23.    radGrid.ItemsSource = data;
24.    radGrid.AutoExpandGroups = true;
25.    radGrid.ShowColumnFooters = true;
26.    radGrid.AutoGenerateColumns = false;
27. 
28.    // Build RadGrid columns
29.    var gridColumns = new[] {user, cat, amt}
30.        .Select(dataColumn => new GridViewDataColumn
31.        {
32.            UniqueName = dataColumn.ColumnName,
33.            DataMemberBinding = new Binding(dataColumn.ColumnName),
34.        })
35.        .ToList();
36.    radGrid.Columns.AddRange(gridColumns);
37. 
38.    // Sum Amount
39.    var amtCol = gridColumns.Last();
40.    var amtFormat = "{0:C}";
41.    amtCol.DataFormatString = amtFormat;
42.    amtCol.AggregateFunctions.Add(new SumFunction()
43.    {
44.        SourceField = amt.ColumnName,
45.        SourceFieldType = amt.DataType,
46.        ResultFormatString = amtFormat
47.    });
48.    radGrid.ShowGroupFooters = true;
49. 
50.    // Group by category
51.    var catGroup = new GroupDescriptor { Member = cat.ColumnName, };
52.    radGrid.GroupDescriptors.Add(catGroup);
53. 
54.    // Show grand total label
55.    radGrid.Columns[0].Footer = "Grand Total";
56. 
57.    // todo: How to show group subtotal label?
58. 
59.}

3 Answers, 1 is accepted

Sort by
0
MC
Top achievements
Rank 1
answered on 02 Dec 2016, 01:22 AM

ok, I've made some progress by using AggregateFunction and a custom AggregationExpression.

But I'm now having problem when I have nested groupings.

Please see the code and screenshots

public MainWindow()
{
    InitializeComponent();
 
    var style = new Style();
    style.Setters.Add(new Setter(GroupHeaderRow.ShowGroupHeaderColumnAggregatesProperty, false));
    style.Setters.Add(new Setter(GroupHeaderRow.ShowHeaderAggregatesProperty, false));
    var radGrid = new RadGridView {GroupRenderMode = GroupRenderMode.Flat, GroupRowStyle = style};
    Content = radGrid;
             
    // Table schema
    var data = new DataTable();
    var dbUser = data.Columns.Add("User");
    var dbType = data.Columns.Add("Type");
    var dbCategory = data.Columns.Add("Category");
    var dbAmt = data.Columns.Add("Amount", typeof(decimal));
 
    // Populate values
    var categories = new[] {"Cat A", "Cat B", "Cat C"};
    var types = new[] {"Trial", "Full"};
    for (var i = 0; i < 20; i++)
    {
        data.Rows.Add(
            "User " + i,
            types[i % types.Length],
            categories[i % categories.Length],
            i * 100d);
    }
 
    // Prepare RadGridView
    radGrid.ItemsSource = data;
    radGrid.AutoExpandGroups = true;
    radGrid.ShowColumnFooters = true;
    radGrid.AutoGenerateColumns = false;
 
    // Build RadGrid columns
    var gridColumns = new[] {dbUser, dbType, dbCategory, dbAmt}
        .Select(dataColumn => new GridViewDataColumn
        {
            UniqueName = dataColumn.ColumnName,
            DataMemberBinding = new Binding(dataColumn.ColumnName),
        })
        .ToList();
    radGrid.Columns.AddRange(gridColumns);
 
    // Sum Amount
    var amtCol = gridColumns.Last();
    var amtFormat = "{0:C}";
    amtCol.DataFormatString = amtFormat;
    amtCol.AggregateFunctions.Add(new SumFunction()
    {
        SourceField = dbAmt.ColumnName,
        SourceFieldType = dbAmt.DataType,
        ResultFormatString = amtFormat
    });
    radGrid.ShowGroupFooters = true;
 
    // Show grand total label
    radGrid.Columns[0].Footer = "Grand Total";
 
    // Nested grouping
    var groupColumns = new[] {
        dbCategory,
        dbType
    };
    foreach (var group in groupColumns)
    {
        var colGroup = new GroupDescriptor {Member = group.ColumnName,};
        radGrid.GroupDescriptors.Add(colGroup);
 
        // Add text to the first column
        // =============================================================
        // (todo) help: Ends up adding 1 function for each nesting level
        var firstColumn = gridColumns.First();
        firstColumn.AggregateFunctions.Add(new AggregateFunction<DataRow, string>
        {
            AggregationExpression = (rows) => $"{rows.First()[group.ColumnName]} subtotal"
        });
    }
}
0
Dilyan Traykov
Telerik team
answered on 05 Dec 2016, 12:43 PM
Hello,

I'm attaching a sample project based on the code you provided where I've achieved the desired behavior by creating a DataTemplateSelector and defining it as the GroupFooterTemplateSelector for the first column. Inside the selector, I get ahold of the row's group like so:

public class MyGroupFooterTemplateSelector : DataTemplateSelector
{
    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var template = new DataTemplate();
        var row = (container as GridViewGroupFooterCell).ParentRow as GridViewGroupFooterRow;
        var groupName = (row.Group as CollectionViewGroup).Name;
        var textBlock = new FrameworkElementFactory(typeof(TextBlock));
        textBlock.SetValue(TextBlock.TextProperty, groupName + " subtotal");
        template.VisualTree = textBlock;
        template.Seal();
        return template;
    }
}

Please let me know whether such an approach would be suitable for you.

Regards,
Dilyan Traykov
Telerik by Progress
Telerik UI for WPF is ready for Visual Studio 2017 RC! Learn more.
0
MC
Top achievements
Rank 1
answered on 07 Dec 2016, 08:26 AM

Hi Dilyan,

Thanks for the reply. Your solution did the trick.

Thanks

Josh

Tags
GridView
Asked by
MC
Top achievements
Rank 1
Answers by
MC
Top achievements
Rank 1
Dilyan Traykov
Telerik team
Share this question
or