PivotGrid - Custom Object Grouping

Drop Filter Fields Here
QuantityColorLine
CategoryClass
BlackBlack Total BrownBrown Total RedRed Total SilverSilver Total WhiteWhite Total YellowYellow TotalGrand Total
A-LineM-LineR-LineT-LineM-LineR-LineT-LineR-LineA-LineM-LineM-LineR-Line
1701   1701      4725 4725    6426
1701   1701      4725 4725    6426
   697697  497497  2487 2487396396  4077
   697697  497497  2487 2487396396  4077
           428 428312312  740
6261121878 2625350583 933207207 250250    4015
 1375821 2196356346 702   112112    3010
62624961699 4821706929 1635207207428362790312312  7765
601   601       9696    697
921817821 2559352344 696   160160  6136134028
1522817821 3160352344 696   256256  6136134725
384933132520697103791058127349728282072077640618825870870861361322993
1 AccessoriesB-Class
1 Accessories Total
2 BikesB-Class
2 Bikes Total
3 WheelsB-Class
H-Class
L-Class
3 Wheels Total
4 OtherL-Class
M-Class
4 Other Total
Grand Total
Isolate this demo as a stand-alone application
  • About this demo
  • C#
  • VB
RadPivotGrid control supports not only grouping by primitive types but it supports grouping by custom objects. By design the PivotGrid shows grouped data which is described into its fields by setting their DataField property. In most cases it is a primitive type (like string, integer, boolean, etc.). However you could set the DataField of some PivotGrid field to a custom object. For example if you have a custom class Product which has property ProductCategory which type is another custom class Category:
public class Product     
{         
   ...         
   public Category ProductCategory { get; set; }     
}
You could set the ProductCategory as DataField of the PivotGrid field and group by Category objects:
...     
<telerik:PivotGridRowField DataField="ProductCategory">     
</telerik:PivotGridRowField>     
... 
In this case you should override the GetHashCode, Equals and ToString() methods into the CategoryClass, also you need to implement IComparable interface and mark the class as Serializable. For example:
[Serializable] public class Category: IComparable 
{     
    public int CategoryID { get; set; }     
    public string CategoryName { get; set; }     
    public string Description {get;set;}      
    public override string ToString()     
    {         
        return CategoryID.ToString() + " " + CategoryName;     
    }      
    public override bool Equals(object obj)     
    {         
        Category category = obj as Category;         
        if (category != null)         
        {             
            return CategoryID.Equals(category.CategoryID) &&                      
            CategoryName.Equals(category.CategoryName) &&                     
            Description.Equals(category.Description);         
        }else{             
            return false;         
        }     
    }      
    public override int GetHashCode()     
    {         
        return CategoryID.GetHashCode() + CategoryName.GetHashCode() + Description.GetHashCode();     
    }      
    public int CompareTo(object obj)     
    {         
        return CategoryID.CompareTo(((Category)obj).CategoryID);     
    } 
}
  • DefaultCS.aspx
  • DefaultCS.aspx.cs
    • DefaultCS.aspx.cs
    • PivotGridDataSourceHelper.cs
<%@ Page Language="c#" AutoEventWireup="false" CodeFile="DefaultCS.aspx.cs" Inherits="Telerik.GridExamplesCSharp.FirstLook.DefaultCS" %>

<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns='http://www.w3.org/1999/xhtml'>
<head runat="server">
    <title>Telerik ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadSkinManager ID="RadSkinManager1" runat="server" ShowChooser="true" />
    <telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server">
    </telerik:RadAjaxLoadingPanel>
    <telerik:RadAjaxPanel ID="radAjaxPanel1" runat="server" LoadingPanelID="RadAjaxLoadingPanel1">
        <telerik:RadPivotGrid RenderMode="Lightweight" ID="RadPivotGrid1"
            runat="server" ColumnHeaderZoneText="ColumnHeaderZone">
            <ColumnHeaderCellStyle Width="80px"></ColumnHeaderCellStyle>
            <Fields>
                <telerik:PivotGridRowField DataField="Category">
                </telerik:PivotGridRowField>
                <telerik:PivotGridRowField DataField="Class">
                </telerik:PivotGridRowField>
                <telerik:PivotGridColumnField DataField="Color">
                </telerik:PivotGridColumnField>
                <telerik:PivotGridColumnField DataField="Line">
                </telerik:PivotGridColumnField>
                <telerik:PivotGridAggregateField DataField="Quantity" Aggregate="Sum">
                </telerik:PivotGridAggregateField>
            </Fields>
            <ClientSettings EnableFieldsDragDrop="true"></ClientSettings>
        </telerik:RadPivotGrid>
    </telerik:RadAjaxPanel>
    </form>
</body>
</html>

Find Assistance

Help Us Improve

Was this example helpful?