GridView排序只能运行一次

时间:2011-03-22 07:53:25

标签: c# asp.net sorting gridview gridview-sorting

我有一个网格,它只按升序排序一次。然后没有任何事情发生。

aspx文件代码:

<asp:GridView ID="gdvSignatureLines" runat="server" CssClass="Grid1" AutoGenerateColumns="False"
                        SkinID="PagedGridView" AllowPaging="True" AllowSorting="True" DataKeyNames="Id" 
                        onrowcommand="gdvSignatureLines_RowCommand" 
                        onrowdeleting="gdvSignatureLines_RowDeleting" 
                        onrowediting="gdvSignatureLines_RowEditing" 
                        onsorting="gdvSignatureLines_Sorting" 
                        onpageindexchanging="gdvSignatureLines_PageIndexChanging">
                        <PagerStyle CssClass="gridPager" HorizontalAlign="Right" />
                        <Columns>
                            <ucc:commandfieldcontrol headertext="Actions" showdeletebutton="true" buttontype="Image"
                                deleteimageurl="~/App_Themes/Default/images/delete.png" showeditbutton="true"
                                editimageurl="~/App_Themes/Default/images/edit.png" deleteconfirmationtext="Are you sure you want to delete?">
                                    <ItemStyle HorizontalAlign="Center" Width="60px" />
                             </ucc:commandfieldcontrol>
                            <asp:BoundField DataField="SortOrder" HeaderText="Line" SortExpression="SortOrder" />
                            <asp:TemplateField HeaderText="Type" SortExpression="glTypeId">
                                <ItemTemplate>
                                    <asp:Label ID="lblglTypeId" runat="server" Text='<%# Eval("GeneralLookup.LookupItem") %>'></asp:Label>
                                </ItemTemplate>  
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Label" SortExpression="glLabelId">
                                <ItemTemplate>
                                    <asp:Label ID="lblglLabelId" runat="server" Text='<%# Eval("GeneralLookup1.LookupItem") %>'></asp:Label>
                                </ItemTemplate>  
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Caption" SortExpression="glCaptionId">
                                <ItemTemplate>
                                    <asp:Label ID="lblglCaptionId" runat="server" Text='<%# Eval("GeneralLookup2.LookupItem") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <div class="divEmptyListingGrid">
                                --- No Signature Line Exists ---
                            </div>
                        </EmptyDataTemplate>
                    </asp:GridView>

cs文件代码:

protected void gdvSignatureLines_Sorting(object sender, GridViewSortEventArgs e)
    {
        lblHeading.Text = "Signature Line for " + reportName;
        ReportOptionsBO reportOptionsBO = new ReportOptionsBO();
        List<ReportSignatureLine> listSignature = reportOptionsBO.GetReportSignatureLineByReportId(reportId);
        if (listSignature != null)
        {
            var param = Expression.Parameter(typeof(ReportSignatureLine), e.SortExpression);
            var sortExpression = Expression.Lambda<Func<ReportSignatureLine, object>>
                (Expression.Convert(Expression.Property(param, e.SortExpression), typeof(object)), param);

            if (e.SortDirection == SortDirection.Ascending)
            {
                gdvSignatureLines.DataSource = listSignature.AsQueryable<ReportSignatureLine>().OrderBy(sortExpression).ToList();
            }
            else
            {
                gdvSignatureLines.DataSource = listSignature.AsQueryable<ReportSignatureLine>().OrderByDescending(sortExpression).ToList();
            }
            gdvSignatureLines.DataBind();
        }
    }

3 个答案:

答案 0 :(得分:4)

由于您在代码中设置了数据源,因此您必须编写一些代码才能使分类工作。

首先,将Sorting事件添加到GridView标记

<asp:GridView ID="gdvSignatureLines" runat="server" CssClass="Grid1" AutoGenerateColumns="False" SkinID="PagedGridView" AllowPaging="True" AllowSorting="True" DataKeyNames="Id" 
   onrowcommand="gdvSignatureLines_RowCommand" 
   onrowdeleting="gdvSignatureLines_RowDeleting" 
   onrowediting="gdvSignatureLines_RowEditing"
   OnSorting="gdvSignatureLines_Sorting"
>

在您的代码隐藏中,绑定排序事件。以下代码使用LINQ对数据进行排序并重新绑定GridView。

using System.Linq.Expressions;

int reportID = 123456;

protected void gdvSignatureLines_Sorting(object sender, GridViewSortEventArgs e)
{
   ReportOptionsBO reportOptionsBO = new ReportOptionsBO();
   List<T> ReportOptionsBOList =  reportOptionsBO.GetReportSignatureLineByReportId(reportId);

   if (ReportOptionsBOList != null)
   {
      var param = Expression.Parameter(typeof(T), e.SortExpression);
      var sortExpression = Expression.Lambda<Func<T, object>>(Expression.Convert(Expression.Property(param, e.SortExpression), typeof(object)), param);

      if (ViewState["SortDirection"] == "DESC")
      {
         ViewState["SortDirection"] = "ASC";
         gdvSignatureLines.DataSource = ReportOptionsBOList.AsQueryable<T>().OrderBy(sortExpression);
      }
      else
      {
         ViewState["SortDirection"] = "DESC";
         gdvSignatureLines.DataSource = ReportOptionsBOList.AsQueryable<T>().OrderByDescending(sortExpression);
      }

      gdvSignatureLines.DataBind();
   }
}

答案 1 :(得分:0)

因为没有数据源,这可能会有所帮助:

http://forums.asp.net/p/956540/1177923.aspx

答案 2 :(得分:0)

由于您在代码隐藏中绑定数据源,因此不会以声明方式更新排序表达式。您需要在代码隐藏中处理更改的排序表达式。

以下是您可以这样做的方法:

using System.Linq.Expressions;

int reportID = 123456;

protected void gdvSignatureLines_Sorting(object sender, GridViewSortEventArgs e)
{
   ReportOptionsBO reportOptionsBO = new ReportOptionsBO();
   List<T> ReportOptionsBOList =  reportOptionsBO.GetReportSignatureLineByReportId(reportId);

   if (ReportOptionsBOList != null)
   {
      var param = Expression.Parameter(typeof(T), e.SortExpression);
      var sortExpression = Expression.Lambda<Func<T, object>>(Expression.Convert(Expression.Property(param, e.SortExpression), typeof(object)), param);

      if (ViewState["SortDirection"] == "DESC")
      {
         ViewState["SortDirection"] = "ASC";
         gdvSignatureLines.DataSource = ReportOptionsBOList.AsQueryable<T>().OrderBy(sortExpression);
      }
      else
      {
         ViewState["SortDirection"] = "DESC";
         gdvSignatureLines.DataSource = ReportOptionsBOList.AsQueryable<T>().OrderByDescending(sortExpression);
      }

      gdvSignatureLines.DataBind();
   }
}