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

[Solved] SortExpression in TemplateColumn by field of related/sub entity TEMP SOLUTION

3 Answers 304 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Juan Angel
Top achievements
Rank 1
Veteran
Juan Angel asked on 20 Apr 2009, 11:32 PM
I read that Hyperlink/TemplateColumn don´t support SortExpression as "[entity].[propertyNameOfRelatedEntity]". BoundColumn it`s ok.

I found a "temp solution" for sort by field of related entity in TemplateColumn. I use a very simple example scenario: Users and Profiles.

  • Entity USER: ID_USER, NAME_USER; ID_PROFILE.
  • Entity PROFILE: ID_PROFILE, NAME_PROFILE
  • The Grid show: ID_USER, NAME_USER, PROFILE.NAME_PROFILE
  • All grid columns enable sorting.

if grid column of NAME_PROFILE it's GridBoundColumn and the SortExpression is "PROFILE.NAME_PROFILE" sort it's OK, but this column is GridTemplateColumn throw an exception: "PROFILE.NAME_PROFILE" column not found. it's true, entity USER don`t contains "PROFILE.NAME_PROFILE" property. 

A possible solution is flatting the results (use an anonymous type, view with join...)

Well, if you put:
  1. A GridBoundColumn with DataField and SortExpression "PROFILE.NAME_PROFILE" with Visible="false"
  2. A GridTemplate with DataField and SortExpression "PROFILE.NAME_PROFILE" (with Visible="true" :-P)
<telerik:RadGrid ID="rgUsuarios" runat="server"  
        AutoGenerateColumns="false" AllowFilteringByColumn="true" AllowSorting="true" 
        <MasterTableView EditMode="InPlace" CommandItemDisplay="Top" DataKeyNames="ID_USUARIO"
            <Columns> 
                ... 
                ... 
                <telerik:GridBoundColumn DataField="PERFIL.DSC_PERFIL" HeaderText="Perfil" SortExpression="PERFIL.DSC_PERFIL" FilterControlWidth="130px" Visible="false"
                    <HeaderStyle    HorizontalAlign="Center"    Width="150px" /> 
                    <ItemStyle      HorizontalAlign="Center" /> 
                </telerik:GridBoundColumn> 
                <telerik:GridTemplateColumn DataField="PERFIL.DSC_PERFIL" HeaderText="Perfil" SortExpression="PERFIL.DSC_PERFIL" FilterControlWidth="130px"
                    <HeaderStyle    HorizontalAlign="Center"    Width="150px" /> 
                    <ItemStyle      HorizontalAlign="Center" /> 
                    <ItemTemplate> 
                        <asp:Label ID="lblDscPerfil" runat="server" Text='<%# Eval("PERFIL.DSC_PERFIL") %>'  /> 
                    </ItemTemplate> 
                    <EditItemTemplate> 
                        <telerik:RadComboBox ID="rcbPerfil" runat="server" Width="95%" /> 
                    </EditItemTemplate> 
                </telerik:GridTemplateColumn> 
                ... 
                ... 
            </Columns> 
        </MasterTableView> 
    </telerik:RadGrid> 
 
 

The click on TemplateColumn Header sort the grid OK. I don`t know the reason, but run OK. 

Conclusion: Adding an invisible GridBoundColum with same Datafield and SortExpression "simulate" the sorting by related field entity it's ok.

I hope that this "patch" will be useful.

3 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 24 Apr 2009, 05:39 AM
Hello Juan,

Can you please try the attached project with latest version of RadControls for ASP.NET AJAX and see whether it works. I believe that the behavior of GridTemplateColumn is proper with latest release.

Greetings,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Andy
Top achievements
Rank 1
answered on 28 May 2009, 09:57 PM
Nikolay,

Your example works (except I had to comment out the LINQ stuff as it didn't work for some reason). However, we are still receiving the "Cannot find column" error for GridDropDownColumns.

It looks as though the DropDown Column will sort on nested properties, but only if the DataField and SortExpression properties match.

I modified Nested Object in your example as such:

public class NestedObject { 
    public int NestedObjectId { getset; } 
    public string Name { getset; } 

My GridDropDownColumn in the aspx looks like this:
<telerik:GridDropDownColumn DataField="Child.NestedObjectId" DataSourceID="ObjectDataSource1" 
HeaderText="DropDown Name" ListTextField="Name" ListValueField="NestedObjectId" 
UniqueName="DropDownName" DropDownControlType="RadComboBox" Visible="true" SortExpression="Child.Name" > 
</telerik:GridDropDownColumn> 

This all works fine until I click on the column header to sort, then it blows with the column not found error.

If I change DataField to "Child.Name" (and ListValueField to "Name" so you can actually get the correct item values from the list) then the sorting works properly. Of course, this isn't all that useful since we need the id of the nested object later on. (Though, we really shouldn't have multiple identical items in the drop down, but that's a different discussion)

Alternatively, we can change the SortExpression to "Child.NestedObjectId" but then it sorts on the ID, which is all sorts of useless for our purposes :)

I haven't tried this with a template column and manually adding a drop down.
0
Nikolay Rusev
Telerik team
answered on 01 Jun 2009, 02:58 PM
Hello Andy,

For your convenience I've prepared sample application that demonstrates sorting on GridDropDownColumn and all seems to be working properly.
Please test the code with latest version of RadControls for ASP.NET AJAX.

Regards,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
Tags
Grid
Asked by
Juan Angel
Top achievements
Rank 1
Veteran
Answers by
Nikolay Rusev
Telerik team
Andy
Top achievements
Rank 1
Share this question
or