79059

GridView Sorting works once only

I have a grid and it is sorting once only in ascending order. Then nothing happens. Please help ASAP !!

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

Please help as soon as possible. Thanks!

Answer1:

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

Answer2:

since it is without datasource, this could help:

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

Answer3:

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

Recommend

  • How to change the gridview width and rename the header after binding data
  • DataGrid TemplateColumn - Triger an event when changes occured(asp:radiobuttonlist)
  • ASP GridView Refresh after Excel Export not working
  • Why cout is producing no output on Code Blocks?
  • Python PIL to extract number from image
  • Cross platform UI spacing/padding
  • Java Application vs. Java Desktop Application in Netbeans [duplicate]
  • Streaming screenshots over WebRTC as a video stream from iOS
  • Cypher - matching two different possible paths and return both
  • get path to groovy source file at runtime
  • Why does it draw lines in the wrong place?
  • git add error : “fatal : malloc, out of memory”
  • How to create CGPath from a SKSpriteNode in SWIFT
  • Switch to popup in python using selenium
  • Redirect to Post Method/Action
  • Javascript, Regex - I need to grab each section of a string contained in brackets
  • Floated image with variable width and heading with background image
  • Is it possible to open regedit and navigate to straight to a specific key using process.start?
  • Adding elements to a huge XML file
  • Moving Android View and preventing onDraw to be called over and over again
  • Webgrid not refreshing after delete MVC
  • Blackberry - Custom EditField Cursor
  • How to have background script and something similar to a default popup?
  • HTTP/2 streams vs HTTP/1.1 connections
  • Android full screen on only one activity?
  • Python urlparse: small issue
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • Django: Count of Group Elements
  • Syntax for setting draggablecursor property in google maps api
  • Adding a button at the bottom of a table view
  • Getting last autonumber in access
  • Fetching methods from BroadcastReceiver to update UI
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Modifying destination and filename of gulp-svg-sprite
  • Symfony2: How to get request parameter
  • Delete MySQLi record without showing the id in the URL
  • GridView Sorting works once only
  • How to get icons for entities from eclipse?
  • WPF Applying a trigger on binding failure