Allowsorting not working in gridview

There are some things I don’t like about gridviews, but one of the things I do like is that all I’ve ever needed to do to add paging and sorting is use the “AllowPaging” and “AllowSorting” attributes. Done.

However, for the first time I’m getting misbehavior. Everything on the page was working fine before I started. All I changed was adding AllowPaging (and PageSize) and Allow Sorting. The paging is working fine, but the column headings did not change and don’t sort. I removed the allowsorting attribute rebuilt the app, then added it back and rebuilt again. Nothing changes at any point. It’s like it’s not recognizing that the sorting attribute is there. Any ideas?

<asp:GridView ID="gvCampaignCookies" runat="server"
    DataSourceID="LinqDataSourceList" AllowPaging="true" PageSize="20"
    AllowSorting="true" CssClass="datagrid" GridLines="None"
        <asp:BoundField DataField="id" HeaderText="id" ReadOnly="true" />
        <asp:BoundField DataField="myid" HeaderText="myid" ReadOnly="true" />
        <asp:BoundField DataField="domain" HeaderText="domain" ReadOnly="true" />
        <asp:BoundField DataField="cid" HeaderText="cid" ReadOnly="true" />
        <asp:BoundField DataField="leadsource" HeaderText="leadsource" ReadOnly="true" />
        <asp:BoundField DataField="leadsourcedetail" HeaderText="leadsourcedetail" ReadOnly="true" />
        <asp:BoundField DataField="aid" HeaderText="aid" ReadOnly="true" />
        <asp:BoundField DataField="productid" HeaderText="productid" ReadOnly="true" />
        <asp:BoundField DataField="url" HeaderText="url" ReadOnly="true" />
	            <asp:LinkButton ID="btnViewDetails" runat="server" Text="Edit" CommandName="Select" />					
	            <asp:LinkButton ID="btnDelete" runat="server" Text="Delete" CommandName="Delete" />

You need to add sort expression to the BoundFields, for example

<asp:BoundField DataField=“myid” HeaderText=“myid” ReadOnly=“true” SortExpression=“myid”/>

Also, why are you using the gridview paging? Remember it fetches all the rows from the database each time you move from page to page within the gridview and that will affect performance if you have lots of rows to deal with. You should consider custom paging, which is quite easy with linq.

Thanks, I’ve never had to use that before, but it worked great.

Agreed. Custom paging all the way. This is a quick emergency patch to resolve a critical issue in an existing application. It’s got ~20 internal users and 100 records. Nor do I want to keep slapping band aids on it. We have a more flexible stringbuilder based admin panel that’s more appropriate for specs of this particular application. So over the next week or so (other projects willing :wink: ) I’ll be replacing this with a new system to does use .Skip() and .Take() methods in a dynamic LINQ app.