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

Apply GridCalculatedColumn's Expression to its Footer

3 Answers 74 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tom Rasmussen
Top achievements
Rank 1
Tom Rasmussen asked on 29 Jun 2012, 06:37 PM
I have a grid that is programmatically created in Page_Load that also includes a varying number of calculated columns.  Each column contains an aggregate (count, sum, etc.).  However, one of my columns is a percentage that is calculated based on other bound columns.  Each row's calculation is done perfectly, but the grid footer should apply the same expression to the rest of the footer's values.  The real trick is that the expression being used by the grid is not known at design time so I am unable to manually create a custom aggregate.  

Anyone have any ideas how I can use the expression and datafields from the GridCalculatedColumn to evaluate that column's footer?

3 Answers, 1 is accepted

Sort by
0
Accepted
Andrey
Telerik team
answered on 04 Jul 2012, 10:37 AM
Hello,

You could achieve your goal by hooking the ItemDataBound event of RadGrid and in its body you could access every item's value for every column. Thus you could calculate your percentage and set ti to the Text property of the FooterItem cell.

More information about this approach could be found in this help topic.

Greetings,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Tom Rasmussen
Top achievements
Rank 1
answered on 05 Jul 2012, 04:33 PM
I figured that would be the necessary course.  I was holding out hope there was some built in part of the control though.  Here was my solution for any others:

protected void StatGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    if (e.Item is GridFooterItem) 
    
        var footerItem = e.Item as GridFooterItem;
        //handle any custom footer aggregates by applying a custom expression based on the other footer columns
        foreach (var field in CalculatedFields.Where(f => f.FooterAggregate == "Custom"))
        {
            //get the list of data fields (columns) used in the expression
            var dataFields = field.Fields.Split(new[] {", "}, StringSplitOptions.RemoveEmptyEntries);
            //get the expression used eg. "iif({0}-{1}<=0,0,{2}/({0}-{1}))"
            var calc = field.Calculation;
            for (var i = 0; i < dataFields.Length; i++)
            {
                //replace the placeholders {x} with the bound data values from the necessary columns, removing any non-numeric characters
                calc = calc.Replace("{" + i + "}", RemoveNonNumeric(footerItem[dataFields[i]].Text));
            }
            //compute the result of the expression
            var result = new DataTable().Compute(calc, null);
 
            //apply the formatting and set the column text
            footerItem[field.ColumnName].Text = String.Format(field.FormatString, result);
        }
    
}
 
private string RemoveNonNumeric(string input)
{
    var regex = new Regex(@"[^0-9\-\.]");
    return regex.Replace(input, String.Empty);
}
0
Andrey
Telerik team
answered on 09 Jul 2012, 03:13 PM
Hello,

Yes, that was the approach I had in mind. I am glad that you have achieved your goal.

Kind regards,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Tom Rasmussen
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Tom Rasmussen
Top achievements
Rank 1
Share this question
or