Apply GridCalculatedColumn's Expression to its Footer

4 posts, 1 answers
  1. Tom Rasmussen
    Tom  Rasmussen avatar
    20 posts
    Member since:
    Jan 2009

    Posted 29 Jun 2012 Link to this post

    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?
  2. Answer
    Andrey
    Admin
    Andrey avatar
    836 posts

    Posted 04 Jul 2012 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Tom Rasmussen
    Tom  Rasmussen avatar
    20 posts
    Member since:
    Jan 2009

    Posted 05 Jul 2012 Link to this post

    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);
    }
  5. Andrey
    Admin
    Andrey avatar
    836 posts

    Posted 09 Jul 2012 Link to this post

    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.
Back to Top