gpt4 book ai didi

c# - GridView 排序只工作一次

转载 作者:行者123 更新时间:2023-11-30 21:16:09 25 4
gpt4 key购买 nike

我有一个网格,它只按升序排序一次。然后什么也不会发生。

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();
}
}

最佳答案

由于您是在后面的代码中设置数据源,因此您必须编写一些代码才能进行排序。

首先,将排序事件添加到 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"
>

在您的代码隐藏中,绑定(bind)排序事件。以下代码使用 LINQ 对数据进行排序并重新绑定(bind) 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();
}
}

关于c# - GridView 排序只工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5388245/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com