Antarr Byrd
Antarr Byrd

Reputation: 26071

Setting AllowSorting To True On GridView Does Not Modify Headers

I'm trying to allow sorting in a GridView control. I set allowSorting to true but it doesn't affect the headings.

Gridview

<asp:GridView ID="gvClaimHistory" runat="server" AutoGenerateColumns="False" CssClass="table table-striped table-bordered table-condensed table-hover smaller-90"
    DataKeyNames="PolicyNumber,Block,Company" Width="100%" AllowPaging="false" AllowSorting="True">
    <Columns>
        <asp:TemplateField HeaderText="">
            <ItemTemplate>
                <asp:Image ID="expandCollapse" runat="server" AlternateText="" CssClass="plus" ImageUrl="~/images/plus.gif" ToolTip="Click to view Claim Details" />
                <asp:Panel ID="pnlClaimDetail" runat="server" Style="display: none">
                    <asp:GridView ID="gvClaimDetail" runat="server" AutoGenerateColumns="false" CssClass="table table-striped table-bordered table-condensed"
                                                    OnRowDataBound="gvClaimDetail_RowDataBound">
                                                    <Columns>
                                                        <asp:TemplateField HeaderText="<%$ Resources:ClaimDetail.LineNumber %>" Visible="false">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblClaimLineNumber" runat="server" Text='<%# Eval("LineNumber")  %>'></asp:Label>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="right" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="<%$ Resources:ClaimDetail.ServiceFromDate %>">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblClaimServiceFromDate" runat="server" Text='<%# Eval("LineIncurDate")  %>'></asp:Label>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="center" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="<%$ Resources:ClaimDetail.ServiceToDate %>">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblClaimServiceToDate" runat="server" Text='<%# Eval("LineIncurToDate")  %>'></asp:Label>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="center" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="<%$ Resources:ClaimDetail.PeriodOfPayment %>" Visible="false">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblDIPaymentPeriod" runat="server"><%# Eval("DIPayPeriodFrom") %> - <%# Eval("DIPayPeriodTo") %></asp:Label>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="center" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="<%$ Resources:ClaimDetail.PolicyInsuredName %>">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblClaimServiceDate" runat="server" Text='<%# StrConv(Eval("PolicyInsuredName"), VbStrConv.ProperCase)  %>'></asp:Label>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="left" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="<%$ Resources:ClaimDetail.ChargeDescription %>">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblClaimServiceDate" runat="server" Text='<%# StrConv(Eval("ChangeTypeDescription"), VbStrConv.ProperCase)  %>'></asp:Label>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="left" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="<%$ Resources:ClaimDetail.ProviderName%>">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblClaimServiceDate" runat="server" Text='<%# StrConv(Eval("ProviderName"), VbStrConv.ProperCase)  %>'></asp:Label>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="left" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="<%$ Resources:Claim.ClaimAmount %>">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblClaimAmount" runat="server" Text='<%# FormatCurrency(Eval("ClaimAmount"),2,,false)  %>'></asp:Label>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="right" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="<%$ Resources:ClaimDetail.PaidDate %>">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblReceivedDate" runat="server" Text='<%# Eval("PaidDate") %>'></asp:Label>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="Center" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="<%$ Resources:ClaimDetail.CheckNumber %>">
                                                            <ItemTemplate>
                                                                <asp:Label ID="lblReceivedDate" runat="server" Text='<%# Eval("CheckNumber") %>'></asp:Label>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="Center" />
                                                        </asp:TemplateField>
                                                        <asp:TemplateField HeaderText="<%$ Resources:ClaimDetail.Status %>">
                                                            <ItemTemplate>
                                                                <% If Me.InsuranceType = AgentResourceDL.DB2.ProductLines.Disability OrElse Me.InsuranceType = AgentResourceDL.DB2.ProductLines.DisabilityIncome _
 OrElse Me.InsuranceType = AgentResourceDL.DB2.ProductLines.GroupDisability Then%>
                                                                <asp:Label ID="lblClaimDetailStatus1" runat="server"> <b><%# StrConv(Eval("StatusDescription"), VbStrConv.ProperCase)%></b><br /> <%# StrConv(Eval("DenialMainDescription"), VbStrConv.ProperCase)%></asp:Label>
                                                                <%Else%>
                                                                <asp:Label ID="lblClaimDetailStatus2" runat="server"> <%# StrConv(Eval("DenialMainDescription"), VbStrConv.ProperCase)%></asp:Label>
                                                                <%End If%>
                                                            </ItemTemplate>
                                                            <ItemStyle HorizontalAlign="left" />
                                                        </asp:TemplateField>
                                                    </Columns>
                                                </asp:GridView>
                </asp:Panel>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="<%$ Resources:Claim.ClaimantNumber %>" Visible="false">
            <ItemTemplate>
                <asp:Label ID="lblClaimantNumber" runat="server" Text='<%# Eval("IndividualDRiderNumber") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="<%$ Resources:Claim.ClaimNumber %>">
            <ItemTemplate>
                <asp:Label ID="lblClaimNumber" runat="server" Text='<%# Eval("ClaimNumber") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="<%$ Resources:Claim.ReceivedDate %>">
            <ItemTemplate>
                <asp:Label ID="lblReceivedDate" runat="server" Text='<%# Eval("ReceivedDate") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="<%$ Resources:Claim.ClaimAmount %>">
            <ItemTemplate>
                <asp:Label ID="lblClaimAmount" runat="server" Text='<%# FormatCurrency(Eval("ClaimAmount"),2,,false)  %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="right" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="<%$ Resources:Claim.AmountDenied %>">
            <ItemTemplate>
                <asp:Label ID="lblAmountDenied" runat="server" Text='<%# FormatCurrency(Eval("DeniedAmount"),2,,false)  %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="right" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="<%$ Resources:Claim.Copay %>">
            <ItemTemplate>
                <asp:Label ID="lblCopay" runat="server" Text='<%# FormatCurrency(Eval("Copay"),2,,false)  %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="right" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="<%$ Resources:Claim.DeductibleAmount %>">
            <ItemTemplate>
                <asp:Label ID="lblDeductibleAmount" runat="server" Text='<%# FormatCurrency(Eval("DeductionAmount"),2,,false)  %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="right" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="<%$ Resources:Claim.LastPaidDate %>">
            <ItemTemplate>
                <asp:Label ID="lblLastPaidDate" runat="server" Text='<%# Eval("PaidDate") %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="right" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="<%$ Resources:Claim.Insured %>" Visible="false">
            <ItemTemplate>
                <asp:Label ID="lblInsured" runat="server" Text='<%# StrConv(Eval("PolicyInsuredName"), VbStrConv.ProperCase)  %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Left" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="<%$ Resources:Claim.LastPaidAmount %>" Visible="false">
            <ItemTemplate>
                <asp:Label ID="lblLastPaidAmount" runat="server" Text=''></asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="right" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="<%$ Resources:Claim.PaidAmount %>">
            <ItemTemplate>
                <asp:Label ID="lblPaidAmount" runat="server" Text='<%# FormatCurrency(Eval("PaidAmount"),2,,false) %>'></asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="right" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="<%$ Resources:Claim.Remarks %>">
            <ItemTemplate>
                <asp:Label ID="lblRemarks" runat="server" Text=''></asp:Label>
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
    </Columns>
    <EmptyDataRowStyle CssClass="EmptyGridRow" />
    <EmptyDataTemplate>
        <div style="width: 98%; vertical-align: middle; padding: 1%;">
            <%= Resources.Common.ThereAreNoRecords.ToString()%>
        </div>
    </EmptyDataTemplate>
</asp:GridView>

Error After Adding Sort Expression

Google Developer Console

Upvotes: 0

Views: 114

Answers (1)

ADyson
ADyson

Reputation: 61849

The MSDN documentation for the AllowSorting property (at https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.allowsorting(v=vs.110).aspx) says:

To enable sorting, set the AllowSorting property to true. When sorting is enabled, the heading text for each column field with its SortExpression property set is displayed as a link button.

and further down

If you define your own columns through the Columns collection, you must set the SortExpression property for each column; otherwise, the column will not display the link button in the header.

You have not set the SortExpression for any of your fields, and therefore they will not be sortable.

Upvotes: 2

Related Questions