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

Group column totals for every column dynamicaly

2 Answers 119 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 16 Mar 2012, 01:04 AM

Is it possible to create group column totals for every column without having to specify individual columns? My grid is actually a pivot table and my columns are generated dynamically by a sql statement that is created by drop down menus. There are 55 different combinations so I don’t want to have to program each individual column. My thought was to specify the group by’s and auto total the rest of the columns. For instance in this example, http://img843.imageshack.us/img843/3719/groupsql.jpg I would specify group by city and sex and the rest of the columns will total automatically. Any ideas will help. Thanks in advance.

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 16 Mar 2012, 09:14 AM
Hello Chris,

You could use Prerender event to calculate the total dynamically. Check the code below:
aspx:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1"ShowGroupPanel="True" onprerender="RadGrid1_PreRender">
   <MasterTableView CommandItemDisplay="Top" DataKeyNames="EmployeeID" ShowGroupFooter="true">
      <GroupByExpressions>
          <telerik:GridGroupByExpression>
               <GroupByFields>
                    <telerik:GridGroupByField FieldName="FirstName" />
                </GroupByFields>
                 <SelectFields>
                    <telerik:GridGroupByField FieldName="FirstName" />
                 </SelectFields>
            </telerik:GridGroupByExpression>
       </GroupByExpressions>
    <Columns>
      <telerik:GridBoundColumn DataField="EmployeeID" UniqueName="EmployeeID" HeaderText="EmployeeID">
      </telerik:GridBoundColumn>
    </Columns>
  </MasterTableView>
</telerik:RadGrid>
C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
 foreach (GridColumn col in RadGrid1.MasterTableView.Columns)
 {
  int length = RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader).Length;
  for (int i = 0; i < length; i++)
  {
   int result = 0;
   GridGroupHeaderItem groupHeader = (GridGroupHeaderItem)RadGrid1.MasterTableView.GetItems(GridItemType.GroupHeader)[i];
   GridItem[] children = groupHeader.GetChildItems();
   foreach (GridDataItem child in children)
   {
      GridDataItem childItem = child as GridDataItem;
      DataRowView DataRow = (DataRowView)child.DataItem;
      result = Convert.ToInt32(DataRow[col.UniqueName].ToString()) + result;
   }
      GridGroupFooterItem groupFooter = (GridGroupFooterItem)RadGrid1.MasterTableView.GetItems(GridItemType.GroupFooter)[i];
      groupFooter[col.UniqueName].Text = result.ToString();
   }
 }
}
Hope this helps.

Regards,
Princy.
0
Chris
Top achievements
Rank 1
answered on 17 Mar 2012, 05:19 AM
This is perfect, Thanks
Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Chris
Top achievements
Rank 1
Share this question
or