Retaining Autosort Sequence

Posted by: rodneybuxton on 8 September 2017, 2:42 pm EST

  • Posted 8 September 2017, 2:42 pm EST

    I have a real basic autosort question: 

    I have a grid that I allow the user to autosort several columns.  Periodically (on a timer event) the entire grid is cleared and rebuilt, when this happens the sort sequence is lost, however the column header still shows that it is sorted by the last column the user selected. How can I maintain the autosort sequence when I reload the grid.  It is unbound...

     Rodney 

  • Replied 8 September 2017, 2:42 pm EST

    On your Timer event you save the column index you are sorting on and use it in the AutoSortColumn Method after you have reloaded your data
  • Replied 8 September 2017, 2:42 pm EST

    Paul,

    At the risk of sounding stupid the second time this morning I guess I knew this is what I wanted to do, but I'm stuck at the VB code part of how to do it.

    I'm really new at spread and am overwhelmed an trying to implement a lot in a little time. 

     How do I get the column index I sorted on out of fpSpread, and then how do I get this AutoSort Column method invoked again after I reload the data?

     Thanks and sorry 

  • Replied 8 September 2017, 2:42 pm EST

    You don't sound stupid at all, in truth I'm not certain how you would get the column index you are sorting on in your Timer event, maybe you could catch the AutoSortingColumn Event and set a global variable you can access anywhere there with the Column index, it is an accessible property of the AutoSortingColumnEventArgs. The AutoSortColumn method is a Public method of the SheetView Class.
  • Replied 8 September 2017, 2:42 pm EST

    Hello,

    Implementing what Paul suggested, I created a sample application  which you may find useful, please have a look. Here instead of the timer click I am using Button click event.

    Public Class Form1

        Dim ds As New DataSet()

        Dim i As Integer

        Dim bool As Boolean

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            bool = True

            Dim emp As New DataTable("Employees")

            Dim div As New DataTable("Division")

            emp.Columns.Add("LastName")

            emp.Columns.Add("FirstName")

            emp.Rows.Add(New Object() {"Jones", "Marianne"})

            emp.Rows.Add(New Object() {"Fieldes", "Anna"})

            div.Columns.Add("Section")

            div.Columns.Add("Specialty")

            div.Rows.Add(New Object() {"Finance", "Taxes"})

            div.Rows.Add(New Object() {"Mergers", "Legal"})

            div.Rows.Add(New Object() {"Sergers", "Megal"})

            div.Rows.Add(New Object() {"Bergers", "Kegal"})

            div.Rows.Add(New Object() {"Xergers", "Segal"})

            div.Rows.Add(New Object() {"Yergers", "Regal"})

            div.Rows.Add(New Object() {"Aergers", "Oegal"})

            ds.Tables.AddRange(New DataTable() {emp, div})

            fpSpread1.DataSource = ds

            FpSpread1.DataMember = "Division"

            FpSpread1.ActiveSheet.Columns(0, 1).AllowAutoSort = True

        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            FpSpread1.Refresh()

            FpSpread1.Reset()

            FpSpread1.DataSource = ds

            FpSpread1.DataMember = "Division"

            FpSpread1.ActiveSheet.SortRows(i, bool, True)

            FpSpread1.ActiveSheet.Columns(i).AllowAutoSort = True

        End Sub

        Private Sub FpSpread1_AutoSortedColumn(ByVal sender As Object, ByVal e As FarPoint.Win.Spread.AutoSortedColumnEventArgs) Handles FpSpread1.AutoSortedColumn

            i = e.Column

            If e.Ascending = True Then

                bool = True

            Else

                bool = False

            End If

        End Sub

    End Class

    Let me know if you don't find it helpful.

     

    Thanks,

     

  • Replied 8 September 2017, 2:42 pm EST

    Wow, way easier than I was making it.  Works like a champ.

     Thanks 

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels