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

Display a COUNT in a Grouping

2 Answers 236 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jason
Top achievements
Rank 1
Jason asked on 13 Oct 2011, 05:25 PM
I have the following RadGrid:

<telerik:RadGrid ID="uxFacilityUsage" runat="server" AllowSorting="True"
        AutoGenerateColumns="False" GridLines="None"
        onneeddatasource="uxFacilityUsage_NeedDataSource" ShowGroupPanel="True"
        onitemdatabound="uxFacilityUsage_ItemDataBound"
        onitemcommand="uxFacilityUsage_ItemCommand" AllowPaging="True"
        PageSize="20" AllowFilteringByColumn="True"
        ongroupschanging="uxFacilityUsage_GroupsChanging">
<HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default"></HeaderContextMenu>
<GroupPanel Text="Drag a column header and drop it here to group by that column."></GroupPanel>
 
<AlternatingItemStyle CssClass="AlternatingRowStyle"></AlternatingItemStyle>
 
<PagerStyle Mode="NextPrevNumericAndAdvanced"></PagerStyle>
<ExportSettings>
    <Pdf PageLeftMargin="0.5in" PageRightMargin="0.5in" />
</ExportSettings>
<MasterTableView CommandItemDisplay="Top" IsFilterItemExpanded="false">
 
<SortExpressions>
                <telerik:GridSortExpression FieldName="StartDate" SortOrder="Descending" />
            </SortExpressions>
<CommandItemSettings ExportToPdfText="Export to Pdf" ShowAddNewRecordButton="False"
        ShowExportToPdfButton="True"></CommandItemSettings>
<GroupByExpressions>
<telerik:GridGroupByExpression>
    <SelectFields>
        <telerik:GridGroupByField FieldName="CylinderType" Aggregate="Count"/>
    </SelectFields>
    <GroupByFields>
        <telerik:GridGroupByField FieldName="CylinderType" FormatString="{0:0}"/>
    </GroupByFields>
</telerik:GridGroupByExpression>
</GroupByExpressions>
<RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>
 
<ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>
    <Columns>
        <telerik:GridBoundColumn DataField="LotNumber"
            FilterControlAltText="Filter LotNumber column" HeaderText="LotNumber"
            SortExpression="LotNumber" UniqueName="LotNumber">
            <HeaderStyle Width="150px" />
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="CylinderType"
            FilterControlAltText="Filter CylinderType column" HeaderText="Cylinder Type"
            SortExpression="CylinderType" UniqueName="CylinderType">
            <HeaderStyle Width="100px" />
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="GasType"
            FilterControlAltText="Filter GasType column" HeaderText="Gas Type"
            SortExpression="GasType" UniqueName="GasType">
            <HeaderStyle Width="100px" />
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Facility"
            FilterControlAltText="Filter Facility column" HeaderText="Facility"
            SortExpression="Facility" UniqueName="Facility" Visible="False">           
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="StartDate"
            DataType="System.DateTime"
            FilterControlAltText="Filter StartDate column"
            HeaderText="Start Date" SortExpression="StartDate"
            UniqueName="StartDate">
            <HeaderStyle Width="150px" />
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="PatientName"
            FilterControlAltText="Filter PatientName column" HeaderText="Patient"
            ReadOnly="True" SortExpression="PatientName" UniqueName="PatientName">
            <HeaderStyle Width="120px" />
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="PayerType"
            FilterControlAltText="Filter PatientName column" HeaderText="Payer Type"
            ReadOnly="True" SortExpression="PayerType" UniqueName="PayerType">
            <HeaderStyle Width="100px" />
        </telerik:GridBoundColumn>         
        <telerik:GridBoundColumn DataField="RoomNumber"
            FilterControlAltText="Filter RoomNumber column" HeaderText="Room"
            SortExpression="RoomNumber" UniqueName="RoomNumber">
            <HeaderStyle Width="50px" />
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="BedAssignment"
            FilterControlAltText="Filter BedAssignment column" HeaderText="Bed"
            SortExpression="BedAssignment" UniqueName="BedAssignment">
            <HeaderStyle Width="100px" />
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Location"
            FilterControlAltText="Filter Location column" HeaderText="Location"
            SortExpression="Location" UniqueName="Location">
            <HeaderStyle Width="150px" />
        </telerik:GridBoundColumn>      
        <telerik:GridBoundColumn DataField="FacilityUsername"
            FilterControlAltText="Filter FacilityUsername column"
            HeaderText="Assigned By" ReadOnly="True" SortExpression="FacilityUsername"
            UniqueName="FacilityUsername">
        </telerik:GridBoundColumn>
    </Columns>
 
<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
</EditFormSettings>
</MasterTableView>
 
<GroupingSettings ShowUnGroupButton="True"></GroupingSettings>
 
        <ClientSettings AllowDragToGroup="True">
 
<Resizing AllowColumnResize="True" EnableRealTimeResize="True"></Resizing>
        </ClientSettings>
 
<FilterMenu EnableImageSprites="False"></FilterMenu>
     
    </telerik:RadGrid>

And my NeedDataSource:

using (var context = new DataContext(SqlHelper.NewConnection))
{
    var results = context.ReportCylinderFacilityUsage(Master.FacilitySelection.SelectedValue);
     uxFacilityUsage.DataSource = results.ToList();
}

ReportCylinderFacilityUsage is a stored procedure.  One of the columns it returns is CylinderType

What I'd like to do is group by CylinderType and have the Count for each type of Cylinder displayed in the header.

When I have the following set up in the GroupBy Expressions:
<telerik:GridGroupByExpression>
    <SelectFields>
        <telerik:GridGroupByField FieldName="CylinderType" />
    </SelectFields>
    <GroupByFields>
        <telerik:GridGroupByField FieldName="CylinderType" Aggregate="Count" />
    </GroupByFields>
</telerik:GridGroupByExpression>
I get an error: FieldName contains invalid characters: count(CylinderType)

if I put the Aggregate on the SelectField then I get the error: A column named "CylinderType" already belongs to this datatable.


Thanks,

Jason

2 Answers, 1 is accepted

Sort by
0
Jason
Top achievements
Rank 1
answered on 13 Oct 2011, 05:37 PM
I've also tried subscribing to the GroupChanging event when the CylinderType is dragged to the panel.

I used the demo at http://www.telerik.com/help/aspnet/grid/radgrid-telerik.webcontrols.radgrid-groupschanging_ev.html as a guide and allowing for the obvious changes in the code I got the : A column named "CylinderType" already belongs to this datatable.


Thanks,

0
Radoslav
Telerik team
answered on 18 Oct 2011, 08:53 AM
Hello Jason,

Could you please try adding the FieldAlias to the SelectFields.GridGroupByField and let me know if the issue still persists:
<GroupByExpressions>
    <telerik:GridGroupByExpression>
           <SelectFields>
                   <telerik:GridGroupByField FieldName="CylinderType" />
            </SelectFields>
            <GroupByFields>
                     <telerik:GridGroupByField FieldName="CylinderType" FieldAlias="CylinderType1" Aggregate="Count" />
             </GroupByFields>
     </telerik:GridGroupByExpression>
</GroupByExpressions>

Also please check out the following online documentation article:
http://www.telerik.com/help/aspnet/grid/radgrid-telerik.webcontrols.gridgroupbyfield-fieldalias.html

Kind regards,
Radoslav
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Jason
Top achievements
Rank 1
Answers by
Jason
Top achievements
Rank 1
Radoslav
Telerik team
Share this question
or