GridView sorting works once only
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/d开发者_Go百科elete.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();
}
}
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
<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"
>
In your code-behind, bind the sorting event. The following code uses LINQ to sort the data and the rebind the 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();
}
}
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:
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();
}
}
精彩评论