Retain the state of the gridview if sorting is done

Hi Experts,

I have a gridview. I added sorting operation to it. If I click any column header it will sort that column. If I click the edit link and go to another page and comeback to the original form then the sorting which I did before is gone. So How can I save the state If I have sorted any column. I am using ASP.Net and VB.Net.

Thanks.

This is what I am using for sorting.

Public Property dir() As SortDirection
        Get
            If ViewState("dirState") Is Nothing Then
                ViewState("dirState") = SortDirection.Ascending
            End If
            Return DirectCast(ViewState("dirState"), SortDirection)
        End Get
        Set(value As SortDirection)
            ViewState("dirState") = value
        End Set
    End Property
    Protected Sub GVViewParts_Sorting(sender As Object, e As GridViewSortEventArgs)
        Dim sortingDirection As String = String.Empty
        If dir = SortDirection.Ascending Then
            dir = SortDirection.Descending
            sortingDirection = "Desc"
        Else
            dir = SortDirection.Ascending
            sortingDirection = "Asc"
        End If
        Dim sortedView As New DataView(BindGridView())
        sortedView.Sort = Convert.ToString(e.SortExpression) & " " & sortingDirection
        GVViewParts.DataSource = sortedView
        GVViewParts.DataBind()
    End Sub
RadhaKrishnaKiJayaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David L. HansenProgrammer AnalystCommented:
I think this may help in your ASP. If not, we can look deeper.
http://www.experts-exchange.com/Programming/Languages/.NET/Q_23043291.html#a20537823
0
RadhaKrishnaKiJayaAuthor Commented:
Hi David,

Thank you for your response. Actually in my case sorting operation is working perfectly. Suppose I sort by customer name and then clicked some other link and went to a different page. When I come back to the original page, what I sorted before is gone. So how can I save the gridview which I have already sorted.

Thank you.
0
David L. HansenProgrammer AnalystCommented:
I understand that the sorting is working. However, explicitly setting the sortexpression may help with the retention issue. It's just a hunch.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

RadhaKrishnaKiJayaAuthor Commented:
I did set the sortexpression already.

<div style="float:right; width:100%;">
       <asp:Panel ID="Panel2" runat ="server" Width="100%"  style="margin-left:5px;">
        <asp:GridView ID="GVCustomers" runat="server" AutoGenerateColumns="false" CellPadding="7" ForeColor="#333333" GridLines="None" width="100%" allowsorting="true"  onsorting="GVCustomers_Sorting">
            <AlternatingRowStyle BackColor="White" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"  />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="white" HorizontalAlign="Center" BorderStyle="Solid" />
            <RowStyle BackColor="#EFF3FB" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />          
            <Columns>
                <asp:TemplateField HeaderText="Cust. Name" SortExpression="CustomerName" >
                    <HeaderStyle Width="40px" HorizontalAlign="left"/>
                    <ItemStyle Width="40px" />

                    <ItemStyle HorizontalAlign="left"></ItemStyle>
                    <ItemTemplate>
                         <asp:Label ID="lblCustID" runat="server" Text='<%# Eval("CustomerID")%>' visible="false"  ></asp:Label>
                          <asp:Label ID="lblCustName" runat="server" Text='<%# Eval("CustomerName")%>'  ></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                 </Columns>
        </asp:GridView>
      </asp:Panel>
    </div>

Thanks.
0
David L. HansenProgrammer AnalystCommented:
Ok, it looks like it must be the viewstate. I'm not an ASP guy so forgive my awkward approach (but nobody else was jumping in to help you, so I thought I may be of some service).

I've found a couple pages to help (if it is a viewstate issue that is). I see you are using "dirState" within the ViewState object, but does't it require an ID to passed through so that the columns' sort state is persistent?

http://www.experts-exchange.com/Programming/Languages/.NET/Q_24867286.html
and
http://www.codeproject.com/Articles/31344/Beginner-s-Guide-To-View-State
0
RadhaKrishnaKiJayaAuthor Commented:
Thank you very much for trying to help me. I will again post it. May be somebody will come forward to help.

Thank u
0
RadhaKrishnaKiJayaAuthor Commented:
I've requested that this question be deleted for the following reason:

Did not get satisfactory answer.

Thank u.
0
Robberbaron (robr)Commented:
if navigating to a different page, you lose all state because its not a postback.

you will have to store the sort parameter into a cookie and retrieve it on page load to set the desired sort field.
0
Duy PhamFreelance IT ConsultantCommented:
Using cookie like Robberbaron suggested is one solution.

Another option could be using the Session variable (https://msdn.microsoft.com/en-us/library/ms178581.aspx), which helps to preserve the sort direction through the user session. Sample code:

    Public Property dir() As SortDirection
         Get
             If Session("dirState") Is Nothing Then
                 Session("dirState") = SortDirection.Ascending
             End If
             Return DirectCast(Session("dirState"), SortDirection)
         End Get
         Set(value As SortDirection)
             Session("dirState") = value
         End Set
     End Property

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RadhaKrishnaKiJayaAuthor Commented:
Thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.