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

Multiple aggregated items in column footer

4 Answers 100 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Phil
Top achievements
Rank 1
Phil asked on 29 May 2014, 07:50 AM
Is it possible to stack the aggregated items in the column footer?

What I am looking for is to display a number of aggregates (sum, avg, std dev) for certain columns that will appear vertically.

4 Answers, 1 is accepted

Sort by
0
Radoslav
Telerik team
answered on 03 Jun 2014, 07:35 AM
Hello Phil,

I am not sure I understand your requirement. If you want to have more than one footer item into the RadGrid, this is not supported. Unfortunately the RadGrid can have only one build in footer item. However you can add manually other footers by using following code:
int lastFooterPos;
    void RadGrid1_PreRender(object sender, EventArgs e)
    {
        AddFooterRow(sender as RadGrid);
    }
 
    private void AddFooterRow(RadGrid grid)
    {
        if (grid != null)
        {
            GridItem[] footerItems = grid.MasterTableView.GetItems(GridItemType.Footer);
 
            if (footerItems.Count() == 1)
            {
                GridTFoot foot = footerItems[0].Parent.Controls[0].Parent as GridTFoot;
 
                for (int i = 0; i < foot.Controls.Count; i++)
                {
                    GridFooterItem item = foot.Controls[i] as GridFooterItem;
 
                    if (item != null)
                    {
                        lastFooterPos = i;
                        break;
                    }
                }
                GridFooterItem existingFooter = foot.Controls[lastFooterPos] as GridFooterItem;
                GridFooterItem newFooterItem = new GridFooterItem(grid.MasterTableView, 0, 0);
 
                foreach (TableCell fc in existingFooter.Cells)
                {
                    TableCell newFooterCell = new TableCell();
                    newFooterCell.Text = "test";
                    newFooterItem.Cells.Add(newFooterCell);
                }
 
                foot.Controls.AddAt(lastFooterPos + 1, newFooterItem);
            }
        }
    }

But in this case you need to calculate aggregates manually and assign them to the newFooterCell.Text property.

I hope this helps.

Regards,
Radoslav
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Phil
Top achievements
Rank 1
answered on 03 Jun 2014, 10:01 AM
Thanks for the reply Radoslav.

What I actually want is a number of aggregate items in certain columns. Imagine a currency value where you may want to see the sum and average and perhaps others in the same column footer.

I can see that  your code works but why do you need to iterate over the cell collection as you are not using the fc object? I have implemented my own code and it does not generate anything unless you iterate that collection.
foreach (TableCell fc in existingFooter.Cells) { TableCell newFooterCell = new
TableCell(); newFooterCell.Text = "test";
newFooterItem.Cells.Add(newFooterCell); }
0
Radoslav
Telerik team
answered on 04 Jun 2014, 08:27 AM
Hi Phil,

Unfortunately the desired functionality is not supported build-in. In order to have more than one aggregate into the footer you need to add additional column with different aggregate. The other approach is to calculate aggregates manually and insert the values into the grid footer.
Additionally the mentioned script which iterates over the cell collection adds values into the cells for each column.

I hope this helps.

Regards,
Radoslav
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Phil
Top achievements
Rank 1
answered on 04 Jun 2014, 08:33 AM
Hi Radoslav,

After some testing I managed to get this resolved with the below code.

private void AddCustomFooter(RadGrid grid, Func<string,string> aggregateFunction, List<string> columnsWithFooter)
{
    GridItem[] footerItems = grid.MasterTableView.GetItems(GridItemType.Footer);
    GridTFoot foot = footerItems[footerItems.Count() - 1].Parent.Controls[0].Parent as GridTFoot;
    GridFooterItem existingFooter = foot.Controls[footerItems.Count() - 1] as GridFooterItem;
 
    GridFooterItem footerItem = new GridFooterItem(grid.MasterTableView, 0, 0);
 
    foreach (TableCell fc in existingFooter.Cells)
    {
        GridTableCell gtc = (GridTableCell)fc;
 
        if (columnsWithFooter.Any(c => c.Contains(gtc.Column.UniqueName)))
        {
            footerItem.Cells.Add(new TableCell() { Text = aggregateFunction(gtc.Column.UniqueName) });
        }
        else
        {
            footerItem.Cells.Add(new TableCell() { Text = string.Empty });
        }
    }
 
    foot.Controls.AddAt(footerItems.Count() - 1, footerItem);
}

The important thing to remember, when using this method, is to assign the text values of the cells that do not require the footer to an empty string or it just writes out one after the other.

Phil


Tags
Grid
Asked by
Phil
Top achievements
Rank 1
Answers by
Radoslav
Telerik team
Phil
Top achievements
Rank 1
Share this question
or