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

Group by derived column

4 Answers 66 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Denise
Top achievements
Rank 1
Denise asked on 25 Jun 2013, 09:12 PM

Hi all,

I have a template column in my grid.  The text of this column is derived by feeding the auditor ID (which is part of the table which is the datasource of the grid) through a function in my code.  This gives me the name of the auditor.  The template column looks like this:

<trt:GridTemplateColumn HeaderText="AuditorId" FooterText ="Total Hours" >
<HeaderTemplate>
<asp:LinkButton ID="btnAuditorId" Text="AuditorId" ToolTip="Sort by Auditor ID"
CommandName='Sort' CommandArgument='AudrId'
runat="server">
</asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblAudDescr" runat="server" Text='<%# ctl.GetAuditorDescr( DataBinder.Eval(Container, "DataItem.AudrId"))%>' />
</ItemTemplate>
</trt:GridTemplateColumn>

This works beautifully to fill my grid, but now I want to use the EnableHeaderContextMenu functionality.  Works well for all the other columns (which are bound columns) but when I try to use it on this column I get an error stating "Expression cannot be null or empty".

I tried setting the groupByExpression in the <trt:GridTemplateColumn> to "ctl.GetAuditorDescr(AudrId)" but then I get an error stating "Invalid group by expression: 'Group By' clause missing".

How can I feed the AudrId, which is part of the datasource, through my GetAuditorDescr function and then group by those results?



4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 26 Jun 2013, 10:57 AM
Hi,

I guess you want to get HeaderContextMenu for a TemplateColumn.RightClick the header and the ContextMenus are shown.
Please try the following code snippet.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server"  AllowSorting="True" AllowMultiRowSelection="False" AllowPaging="True" AllowFilteringByColumn="true">
    <MasterTableView  AllowMultiColumnSorting="True" EnableHeaderContextMenu="true"
        AllowNaturalSort="true" EnableHeaderContextFilterMenu="true">
        <Columns>
            <telerik:GridBoundColumn SortExpression="CustomerID" HeaderText="CustomerID" DataField="CustomerID"
                UniqueName="CustomerID">
            </telerik:GridBoundColumn>          
            <telerik:GridTemplateColumn DataField="City" UniqueName="City" HeaderText="City">
                <HeaderTemplate>
                    <asp:LinkButton ID="lnkSort" runat="server" CommandArgument="City" CommandName="Sort"
                        Text="City"></asp:LinkButton>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblCity" runat="server" Text='<%#Eval("City") %>'></asp:Label>
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>  
</telerik:RadGrid>

Thanks,
Princy
0
Denise
Top achievements
Rank 1
answered on 26 Jun 2013, 05:21 PM
Not quite.  The problem is that the auditor name is not contained in the dataset that is the source for this grid.  The auditor ID is there, but not the name.  To get the name, I feed the ID (which is different column in the grid) through the GetAuditorDescr routine that I wrote. 

So, for instance, if i have an auditor ID of 1 for that record, then GetAuditorDescr(1) returns "Tom" and "Tom" is what is used as the text of my ASPLabel which is in the ItemTemplate for that column.  But becuase "Tom" (the Auditor Description field) does not exist in that dataset, then I can't set it as a DataField for the template column.

Using GetAuditorDescr to pull the name of the auditor based on the ID is working perfectly.  The grid shows the auditor ID in one column and the auditor name in another.  Only thing is the grouping.  I want to be able to group by the name, not the ID.

Does that help clear it up?
0
Andrey
Telerik team
answered on 28 Jun 2013, 01:49 PM
Hi,

RadGrid applies all of its advanced operations directly on the datasource. So, when you try to group given column the grouping engine takes the name of the DataField property which is specified in the column definition and then build the groups based on the value of this data field. This is done through a completely different request to the database to which you do not have access to.

In order to achieve your goal you should join both tables into one result set and then set the DataField property of the GridTemplateColumn to the Name you want to be grouped on, not the ID.

Regards,
Andrey
Telerik
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 the blog feed now.
0
Denise
Top achievements
Rank 1
answered on 01 Jul 2013, 05:32 PM
Thanks!  While it wasn't the answer I wanted (had to rip out a bunch of stuff and rework my logic) it was what I needed to hear.  Thanks again for getting on this quickly and helping me move things along.
Tags
Grid
Asked by
Denise
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Denise
Top achievements
Rank 1
Andrey
Telerik team
Share this question
or