Skip to main content Skip to footer

HowTo: Customize GroupHeaderRow in Spread for ASP.NET

Spread for ASP.Net provides built-in functionality to perform Grouping in Spread that allows users to group the data rows by a specific column. Use the AllowGroup property of the sheet to turn on grouping. The group header row by default displays the column index and the unique value in the column on the basis of which the specific group is created. This blog discusses how we can customize the group header row text to display the row count of each group or subgroup in the group header row along with the default text.

Implementation

The implementation is quite simple. All we need is to loop through the rows in Spread's active sheet view after grouping has been performed to check if the specific row is a group header row or a data row. If the row is a group header row, we customize its text with the help of the following code. This customization routine is called from the Grouped event of Spread which is raised when the user is done with grouping.


private void CalculateGroups(object sender)  
{  
   FarPoint.Web.Spread.FpSpread ss = (FarPoint.Web.Spread.FpSpread)sender;  
   FarPoint.Web.Spread.Model.GroupDataModel gm;  
   int total;  
   int column = 0;  
   int y = 0;  
   gm = (FarPoint.Web.Spread.Model.GroupDataModel)ss.ActiveSheetView.DataModel;  

   for (int i = 0; i < ss.ActiveSheetView.NonEmptyRowCount; i++)  
   {  
     if (gm.IsGroup(i))  
      {  
        FarPoint.Web.Spread.Model.Group g;  
        g = gm.GetGroup(i);  
        g.Expanded = false;  
        total = 0;  
        total = g.Rows.Count;  
        column = g.Column;  

        string s = gm.TargetModel.GetValue(getRow(g), column).ToString();  
        s = column.ToString() + ":" + s;  
        g.Text = s + " (" + total.ToString() + ")";  
      }  
   }  
}  

int getRow(FarPoint.Web.Spread.Model.Group group)  
{  
  if (group.Rows[0] is FarPoint.Web.Spread.Model.Group) return getRow(group.Rows[0] as FarPoint.Web.Spread.Model.Group);  
      return (int)group.Rows[0];  
}  

Here is how the grid looks after customizing : Output Refer to the attached samples for complete implementation. Download C# Sample Download VB Sample

MESCIUS inc.

comments powered by Disqus