2

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();
        }
    }
halfer
  • 18,701
  • 13
  • 79
  • 158
asma
  • 2,677
  • 13
  • 57
  • 87
  • Why do you not want to handle to sort event? – NakedBrunch Mar 21 '11 at 13:14
  • because I don't know how to handle these events as I am using a List, not a datatable. Please see the code. this.gdvSignatureLines.DataSource = reportOptionsBO.GetReportSignatureLineByReportId(reportId); this.gdvSignatureLines.DataBind(); – asma Mar 21 '11 at 14:11
  • It doesn't really matter to the gridview and sorting what type of data structure you are using to bind the gridview. The sorting code will work with a list. I'll update my answer with the exact code you can use in your project. – NakedBrunch Mar 21 '11 at 15:11
  • I fail to see why the link to that blog is relevant – Steven Ryssaert Mar 22 '11 at 09:08
  • If you have the solution of my problem, please do let me know ! – asma Mar 22 '11 at 09:40
  • This question is a duplicate for http://stackoverflow.com/questions/5377777/gridview-sorting-is-not-working If you continue there, i'm sure people will help you. – Steven Ryssaert Mar 22 '11 at 09:48
  • yes that is posted by me but no one is replying there coz I think they can't easily see the comments and they read just the problem. Not the comments. I have almost solved the prob. Just one thing remaining I mentioned here in a new post. – asma Mar 22 '11 at 09:50
  • @asma: Welcome to StackOverflow, please review the [faq](http://stackoverflow.com/faq). I'd also suggest you read [this blog post](http://blog.stackoverflow.com/2010/10/asking-better-questions/) for hints on how to ask better questions. Re-asking the same question isn't acceptable behavior here. If you want more attention for your question, you can [place a bounty on it](http://stackoverflow.com/faq#bounty) after two days. You can also [edit your question](http://stackoverflow.com/posts/5219937/edit) to add additional information, which may make your question easier to understand and answer. –  Mar 22 '11 at 17:13

3 Answers3

4

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();
   }
}
NakedBrunch
  • 45,899
  • 13
  • 69
  • 97
  • Thanks Alison, I'll soon use this code and then I'll let you know. Thanks once again ! – asma Mar 22 '11 at 05:41
  • There occur an exception: The data source does not support server-side data paging. – asma Mar 22 '11 at 06:46
  • Hey Alison, the only problem left now is, sorting is performing only for the first time. Not for the next time. Can you help once again please? – asma Mar 22 '11 at 07:03
  • If you add breakpoints to your code, are you able to hit them? – NakedBrunch Mar 22 '11 at 12:43
  • Yes, the problem I found is, sortexpression always contain ascending. So it seems that first time it is sorting and not for the next time. In fact it is sorting in ascending order always. – asma Mar 22 '11 at 12:48
  • Because the data source is being set in code behind, the sort expression is not being updated declaratively. We need to account for the sort direction change by storing the sort direction in ViewState. I've update my answer and this should get you there. Feel free to upvote my answer if you think it helps you. – NakedBrunch Mar 22 '11 at 13:40
  • You are so helpful Alison !! Thanks a lot. This would definitely help me out. Thanks a lotttttt ... :) – asma Mar 22 '11 at 13:43
  • Fantastic. I know the feeling. Welcome to StackOverflow. It's a great resource. I also added an answer to your other/similar gridview sorting question. – NakedBrunch Mar 22 '11 at 13:54
0

since it is without datasource, this could help:

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

Robert
  • 3,190
  • 1
  • 13
  • 25
0

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();
   }
}
NakedBrunch
  • 45,899
  • 13
  • 69
  • 97