GridView 排序仅有效一次
我有一个网格,它仅按升序排序一次。然后什么也没有发生。
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();
}
}
I have a grid and it is sorting once only in ascending order. Then nothing happens.
Code of aspx file:
<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>
Code of cs file:
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您在后面的代码中设置数据源,因此您必须编写一些代码才能进行排序。
首先,将排序事件添加到 GridView 标记中
在代码隐藏中,绑定排序事件。以下代码使用 LINQ 对数据进行排序并重新绑定 GridView。
Since you're setting the datasource in code behind then you'll have to write a bit of code to make sorting work.
First, add the Sorting event to the GridView markup
In your code-behind, bind the sorting event. The following code uses LINQ to sort the data and the rebind the GridView.
由于它没有数据源,这可能会有所帮助:
http://forums.asp.net/p /956540/1177923.aspx
since it is without datasource, this could help:
http://forums.asp.net/p/956540/1177923.aspx
由于您在代码隐藏中绑定数据源,因此排序表达式不会被声明性更新。您需要在代码隐藏中处理不断变化的排序表达式。
您可以这样做:
Since you are binding the datasource in your code-behind, the sort expression is not being declaratively updated. You need to handle the changing sort expression in code-behind.
Here's how you can do it: