user2107373
user2107373

Reputation: 437

ASP.NET Dynamic Data Sorting on IDForeign Key

In my application we are using ASP.NET Dynamic Data for data binding. Everything works fine.

If you see the code snipped below, it has Dynamic Field DepartmentID which is a foreign key to Department table and gets the department name from there (this is specified in DynamicData/FieldTemplates/IDForeignKey.ascx.cs).

Our requirement is to sort (order by) the records on the department name as they are visible on the grid view. The only option that I have is to sort on DepartmentID which does not serve our purpose.

How can we sort on data that is retrieved from ForeignKey?

List.aspx

       <asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" OnPreRender="GridView1_PreRender"
            AllowPaging="True" AllowSorting="True" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="EditHyperLink" runat="server"
                            NavigateUrl='<%# table.GetActionPath(PageAction.Edit, GetDataItem()) %>'
                        Text="Edit" />&nbsp;|&nbsp;<asp:LinkButton ID="DeleteLinkButton" runat="server" CommandName="Delete"
                            CausesValidation="false" Text="Delete"
                            OnClientClick='return confirm("Are you sure you want to delete this item?");'
                        />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:DynamicField DataField="DepartmentID" HeaderText="Department" />                    
                <asp:DynamicField DataField="Description" HeaderText="Description"/>
            </Columns>

            <PagerStyle/>        
            <PagerTemplate>
                <asp:GridViewPager runat="server" />
            </PagerTemplate>
            <EmptyDataTemplate>
                There are currently no items in this table.
            </EmptyDataTemplate>
        </asp:GridView>
    </div>
        <asp:EntityDataSource OnSelecting="GridDataSource_Selecting" ID="GridDataSource"  runat="server" EnableDelete="true" OnDeleted="GridDataSource_Deleted"  >
            <WhereParameters>
                <asp:DynamicControlParameter ControlID="DynamicFilter1" />
            </WhereParameters>
        </asp:EntityDataSource>

Upvotes: 1

Views: 377

Answers (1)

Konstantin
Konstantin

Reputation: 806

Unfortunately it is not entirely clear from your question how are describe your metadata,
but anyway you can try to use QueryExtender control and SearchExpression class. More detail information you can find by link QueryExtender and SearchExpression with examples.

The whole idea of this is use custom LINQ.

Upvotes: 1

Related Questions