Jazzy
Jazzy

Reputation: 519

Sort a DropdownLIst populated by ObjectDataSource

I am populating a DropdownLIst from an ObjectDataSource. The ObjectDataSource gets it values from a service. The service delivers the results unsorted. Is there way I can sort the by DataTextField before the data is bound to the DropdownList? I feel like there should be a way to do this within the client-side here, but I don't know how. Suggestions please? (.net 4.5)

        <asp:DropDownList ID="AffiliationDropDownList" runat="server"
            ClientIDMode="Static" Width="200px" 
            DataSourceID="AffiliationObjectDataSource" 
            DataTextField="AffiliateName" 
            DataValueField="AffiliateID" 
            AppendDataBoundItems="true" 
            AutoPostBack="false" >
            <asp:ListItem Text="All Affiliations" Value="" />
        </asp:DropDownList>
        <asp:ObjectDataSource runat="server" ID="AffiliationObjectDataSource" 
            SelectMethod="RetrieveActiveAffiliations" 
            TypeName="OnlineVolunteerApplication.Data.VolunteerService.VolunteerServiceClient" >
        </asp:ObjectDataSource>

Upvotes: 1

Views: 529

Answers (2)

Rahul Tripathi
Rahul Tripathi

Reputation: 172428

You can try doing this:-

DataView defaultView = dataSource.Tables[0].DefaultView;
DataTable dt = getData();
dt.DefaultView.Sort= "UserName DESC";

Upvotes: 0

Ehsan
Ehsan

Reputation: 32681

you can do it like this in your select method RetrieveActiveAffiliations. You can see the details here

DataView dv = new DataView(yourtable);
dv.Sort = "id asc"; //your sort expression

Upvotes: 1

Related Questions