Telerik - Custom Group Footers In RadGrid

As anyone that has dealt with this knows Telerik only supports a single Group or Header Template.  So what happens when you want to change them?  You really only have two options from what I have seen.  The first option is to have something in the data that is brought back that you can use in the designer code in the form of a conditional.  Too bad I do not see a way to check the data based on level like some sort of ParentID like some people have done.  If the ParentID is null  It would look something like below where you check it in the designer:

<asp:Label runat="server" ID="Label2" Text='<%# "Number of items in group: "+ (((GridGroupHeaderItem)Container).AggregatesValues["AvailableUnits"]) %>' Visible='<%# ((((GridGroupHeaderItem)Container).AggregatesValues["AvailableUnits"]) != null)%>'>


Or you can use the code behind to really get at the groupIDs like this person did in this code.

protected void TrackersRadGrid_ItemDataBound(object sender, GridItemEventArgs e)
       {
           if (e.Item is GridGroupFooterItem)
           {
               //get the footer cell:
               var portfolioNumberCell = (e.Item as GridGroupFooterItem)["PortfolioName"];
               //find the group header for the current group:
               var headerItem = TrackersRadGrid.MasterTableView.GetItems(GridItemType.GroupHeader)
                   .Where(i => i.GroupIndex == e.Item.GroupIndex).First() as GridGroupHeaderItem;
 
               string groupHeaderText = headerItem.DataCell.Text;
               //get just the left part - only group level but not the actual value:
               string groupLevel = groupHeaderText.Substring(0, groupHeaderText.IndexOf(':'));
               //add group level to the text in the footer:
               portfolioNumberCell.Text = string.Format("{0} {1}", groupLevel, portfolioNumberCell.Text);
}


For my needs I needed to modify the GroupFooterTemplate based on Group Level.  GroupID is not available from the client side.  Furthermore the top level grouping will end up dynamic in my case.  A programmer friend was able to do something like this with Master Detail records but he never did two levels.

  Looking at the GroupId at the first and second level of my grid I was able to see that the IDs are of the form "X" for the first level and form "X_X" for the second level.  So you could for instance do this if you are only interested in the first level:

 if (e.Item is GridGroupFooterItem)
            {
                if (!e.Item.GroupIndex.Contains("_")) //top level is indexed 1,2,3, etc.  Next level is 0_1,0_2, so contains a "_". should be faster than all that linq stuff.
                {
                    //to modify something
                    GridGroupFooterItem groupFooter = (GridGroupFooterItem)e.Item;
                    var at = (Label)groupFooter.FindControl("lblGFColName");

                    if (at != null)
                    {
                        at.Text = string.Empty;
                    }
}


This faster check might work badly if they change that format.



http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/functionality/grouping/group-header-and-footer-templates

http://www.telerik.com/forums/displaying-group-name-in-the-group-footer

http://stackoverflow.com/questions/15599354/telerik-asp-net-ajax-radtreeview-display-hierarchical-data-from-two-joined-table

Comments

Popular posts from this blog

Asp.net Publishing Broke Site - "App_WebReferences is not allowed because the application is precompiled"