Question

My searches thus far haven't turned up anything fruitful - I suspect it's that I'm just not fluent in the terminology. How do I sort by what's displayed in the grid view, rather than the data that's behind it?

For example, my SQL datasource has a list of departments (stored as numbers). When I display the departments on the gridview, I call a function that does a lookup and returns the department name:

<asp:TemplateField HeaderText="Department" Visible="True" DataField="CurrentDepartment" SortExpression="CurrentDepartment">
    <ItemTemplate>
        <asp:Label ID="LabelDepartment" runat="server" Text='<%# gclass.GetDepartmentNameByDeptNumber(Eval("CurrentDepartment"))%>'
    </ItemTemplate>
</asp:TemplateField>

Sorting this column orders the results by the numeric representation of the departments.

I run into a similar problem when I'm using Telerik's RadGrid with filters. I can search by the numeric values, but not the displayed values:

<telerik:GridTemplateColumn HeaderText="Department" ItemStyle-Width="175px" FilterControlWidth="165px" Visible="True" DataField="CurrentDepartment" SortExpression="CurrentDepartment" AutoPostBackOnFilter="true" CurrentFilterFunction="Contains">
    <ItemTemplate>
       <asp:Label ID="LabelDepartment" runat="server" Text='<%# gclass.GetDepartmentNameByDeptNumber(Eval("CurrentDepartment"))%>' Visible="true"></asp:Label>
    </ItemTemplate>
</telerik:GridTemplateColumn>

Suggestions and links are welcome!

Was it helpful?

Solution

For anyone that comes across this looking for the same answer, I never did come across how to change the sort/filter logic.

What I ended up doing was modifying my Stored Procedure that returns the data.
Instead of pulling from just the one table, I joined 4 tables and returned all the columns I needed.

So instead of calling a function to display the data in an ItemTemplate/GridTemplateColumn, I can now just use a BoundField/GridBoundColumn and display straight from each of the tables.
This allows the sort & filter to work without modification.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top