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

Custom 2-Level Subtotals in RadGrid Header & Footer

2 Answers 147 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Matthew
Top achievements
Rank 1
Matthew asked on 20 Apr 2010, 08:54 PM
Hello All,

I've been tasked with an interesting scenario from our business unit, and tried to address it with RadGrid, but after the work I've done so far, I'm not sure I'm going to walk away with a solution that is going to work.

Basically, I need to make grid that looks like an excel mockup. Attached is the mockup.png file, and I've been testing with AdventureWorks, so this isn't the real data, but a pretty close idea. I need to subtotal and expand/collapse at one level, and display the subtotal above the details, and then inside of those details subtotal (but without expand/collapse), and display those subtotals below their details. I don't need subtotals both above and below for either levels. I started working on it, and the attached wip.png is a screenshot of what I have now, but the problem I have is the group header row is not really part of the table, it is inside of a <p> tag inside of a <td> with colspan=4.

I have the complete code, too, but I'm not allowed to upload .zip files. I'll put the code in another reply, but if you'd like the complete code to play with, please email me (mhowell at newportgroup dot com) or PM(?) me.

Thanks,

Matthew

2 Answers, 1 is accepted

Sort by
0
Matthew
Top achievements
Rank 1
answered on 20 Apr 2010, 08:55 PM
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
 
<html xmlns="http://www.w3.org/1999/xhtml"
<head id="Head1" runat="server"
    <title>Subtotal Radgrid Test</title> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> 
    <script type="text/javascript"
    </script> 
</head> 
<body> 
    <form id="form1" runat="server"
    <div> 
        <asp:ScriptManager ID="ScriptManager1" runat="server" /> 
        <rad:RadGrid ID="rg" runat="server" DataSourceID="ds" Width="500px" OnItemDataBound="rg_ItemDataBound" OnPreRender="rg_PreRender" 
          GridLines="None" OnColumnCreated="rg_ColumnCreated"
          <HeaderContextMenu EnableAutoScroll="True"></HeaderContextMenu> 
 
          <MasterTableView AutoGenerateColumns="false" ShowGroupFooter="true"
            <Columns> 
              <rad:GridBoundColumn DataField="Name" HeaderText="Name" /> 
              <rad:GridBoundColumn DataField="Color" HeaderText="Color" Aggregate="Max" FooterAggregateFormatString="Total for {0}:" /> 
              <rad:GridBoundColumn DataField="ListPrice" HeaderText="Price" DataFormatString="{0:c}" Aggregate="Sum" FooterAggregateFormatString="{0:c}" /> 
            </Columns> 
            <GroupByExpressions> 
              <rad:GridGroupByExpression> 
                <GroupByFields> 
                  <rad:GridGroupByField FieldName="LessThanPrice" /> 
                </GroupByFields> 
                <SelectFields> 
                  <rad:GridGroupByField FieldName="LessThanPrice" FormatString="Less than {0:c}" /> 
                  <rad:GridGroupByField FieldName="ListPrice" FieldAlias="Price" FormatString="{0:c}" Aggregate="Sum" /> 
                </SelectFields> 
              </rad:GridGroupByExpression> 
              <rad:GridGroupByExpression> 
                <GroupByFields> 
                  <rad:GridGroupByField FieldName="Color" /> 
                </GroupByFields> 
                <SelectFields> 
                  <rad:GridGroupByField FieldName="Color" FormatString="{0}" FieldAlias="RemoveThisGroupbyRow" /> 
                  <rad:GridGroupByField FieldName="ListPrice" FieldAlias="Price" FormatString="{0:c}" Aggregate="Sum" /> 
                </SelectFields> 
              </rad:GridGroupByExpression> 
            </GroupByExpressions> 
          </MasterTableView> 
        </rad:RadGrid> 
        <asp:SqlDataSource ID="ds" runat="server"  
          ConnectionString='<%$ ConnectionStrings:AdventureWorks %>'  
          ProviderName="System.Data.SqlClient"  
          SelectCommand="select ProductID, Name, Color, ListPrice, LessThanPrice = ceiling(ListPrice / 10) * 10 from Production.Product where Color is not null order by Color, ListPrice, Name"></asp:SqlDataSource> 
    </div> 
    <rad:RadAjaxManager runat="server"
      <AjaxSettings> 
        <rad:AjaxSetting AjaxControlID="rg"
          <UpdatedControls> 
            <rad:AjaxUpdatedControl ControlID="rg" /> 
          </UpdatedControls> 
        </rad:AjaxSetting> 
      </AjaxSettings> 
    </rad:RadAjaxManager> 
    </form> 
</body> 
</html> 
 




using System.Diagnostics; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using Telerik.Web.UI; 
 
public partial class _Default : System.Web.UI.Page 
    protected void Page_Load(object sender, EventArgs e) 
    { 
 
    } 
    protected void rg_PreRender(object sender, EventArgs e) 
    { 
        var rows = rg.MasterTableView.GetItems(GridItemType.GroupFooter, GridItemType.GroupHeader); 
        for(var i = 0; i < rows.Count(); i++) 
        { 
            if(rows[i] is GridGroupHeaderItem) 
            { 
                var hdr = rows[i] as GridGroupHeaderItem; 
                //Debug.WriteLine(hdr.DataCell.Text); 
                if(hdr.DataCell.Text.Contains("RemoveThisGroupbyRow:")) 
                { 
                    hdr.Visible = false
                    continue
                } 
 
                hdr.DataCell.ColumnSpan = 1; 
 
 
                //hdr.DataCell.Text = hdr.DataCell.Text.Replace("Color: ", "").Replace("Price: ", ""); 
            } 
            else if(rows[i] is GridGroupFooterItem && rows[i - 1] is GridGroupFooterItem) 
            { 
                rows[i].Visible = false
            } 
        } 
 
    } 
    protected void rg_ItemDataBound(object sender, GridItemEventArgs e) { } 
 
    bool swap = true
    protected void rg_ColumnCreated(object sender, GridColumnCreatedEventArgs e) 
    { 
        if(e.Column is GridGroupSplitterColumn) 
        { 
            e.Column.Visible = swap; 
            swap = !swap; 
        } 
    } 
 






0
Radoslav
Telerik team
answered on 23 Apr 2010, 12:31 PM
Hello Matthew,

I have reviewed the code which you post. To achieve the desired functionality you could try hiding the expand collapse images for the inner groups. Please check out the following online documentation article which explains how to hide the expand/collapse images:
http://www.telerik.com/help/aspnet-ajax/grdpreventgroupsexpansion.html

I hope this helps.

All the best,
Radoslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Matthew
Top achievements
Rank 1
Answers by
Matthew
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or