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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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. HansenCEOCommented:
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
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.
David L. HansenCEOCommented:
I understand that the sorting is working. However, explicitly setting the sortexpression may help with the retention issue. It's just a hunch.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

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.
David L. HansenCEOCommented:
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
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
RadhaKrishnaKiJayaAuthor Commented:
I've requested that this question be deleted for the following reason:

Did not get satisfactory answer.

Thank u.
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.
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

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.
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.