Sort DataModel and ViewModel

Posted by: afallas on 8 September 2017, 1:02 pm EST

  • Posted 8 September 2017, 1:02 pm EST


    How can I sort the Data Model and View Model at the same time? I need to sort the View Model because I'm changing the background color of some rows; so I should use SortRows. I also need to sort the Data Model because the user can export the grid to Excel (I should use SortRange).


  • Replied 8 September 2017, 1:02 pm EST


    There is no "SaveAsViewed" flag. I tried "SaveAsFiltered" but didn't work.

    I don't know if I'm getting something wrong but I tought SortRows sorts the view only and SortRange sorts the DataModel.

  • Replied 8 September 2017, 1:02 pm EST

    The SaveAsViewed flag is only in version 4.  The SortRange will sort both.
  • Replied 8 September 2017, 1:02 pm EST

    I have the following code and it doesn't work (Win Spread v3.0.2005.2005):

    <FONT size=2>

    sp.ActiveSheet.RowCount = 10

    sp.ActiveSheet.ColumnCount = 1

    </FONT><FONT color=#0000ff size=2>For</FONT><FONT size=2> i </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Integer</FONT><FONT size=2> = 0 </FONT><FONT color=#0000ff size=2>To</FONT><FONT size=2> 9

    sp.ActiveSheet.Cells(i, 0).Text = 10 - i

    </FONT><FONT color=#0000ff size=2>Next

    </FONT><FONT size=2>

    sp.ActiveSheet.Rows(0).BackColor = Color.Chocolate

    sp.ActiveSheet.Rows(4).BackColor = Color.CornflowerBlue

    </FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> si </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> FarPoint.Win.Spread.SortInfo() = {</FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> FarPoint.Win.Spread.SortInfo(0, </FONT><FONT color=#0000ff size=2>True</FONT><FONT size=2>)}

    sp.ActiveSheet.SortRange(0, 0, sp.ActiveSheet.RowCount, sp.ActiveSheet.ColumnCount, </FONT><FONT color=#0000ff size=2>True</FONT><FONT size=2>, si)</FONT>

    After the call to SortRange the rows 0 and 4 that have a different backcolor stay in the same position. They should move with the data. I can achieve that with SortRows but then when the data is exported to Excel it appears unsorted. Hence, SortRange only sorts the Data not the View.

    <FONT size=2> 

  • Replied 8 September 2017, 1:02 pm EST


    Your code is only sorting the cells in the Spread, but these colors are associated with the Rows. In order to sort the entire row and not just the data in the row, you need to change your call to SortRange.

    		<SPAN class=kwd>Dim</SPAN> si <SPAN class=kwd>As</SPAN> FarPoint.Win.Spread.SortInfo() = {<SPAN class=kwd>New</SPAN> FarPoint.Win.Spread.SortInfo(0, <SPAN class=kwd>True</SPAN>)}

    sp.ActiveSheet.SortRange(-1, -1, -1, -1, <SPAN class=kwd>True</SPAN>, si)
  • Replied 8 September 2017, 1:02 pm EST

    Thank you, this solved my problem. Perhaps it will  be good to document this behavior in the SortRange method. Currently the documentation doesn't say anything about the use of -1 as arguments.
  • Replied 8 September 2017, 1:02 pm EST

    Use the SortRange method.
  • Replied 8 September 2017, 1:02 pm EST


    You can use the SortRange method as Bob stated to sort the View and the DataModel. However, this is slow. I would suggest sticking with the SortRows method and then when saving to Excel using the "SaveAsViewed" ExcelSaveFlag to save the data sorted.

Need extra support?

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

Learn More

Forum Channels