FlexGrid sort iComparer

Posted by: fsegui on 21 February 2022, 1:12 pm EST

  • Posted 21 February 2022, 1:12 pm EST

    Hi,

    I try to use a specific iComparer to sort a FlexGrid, but it’s not taken into account. The column is not sorted at all.

    See sample attached.

    GroupPanelUpdate_FG.zip

    Thanks,

    Regards

  • Posted 22 February 2022, 4:29 am EST

    Hi,

    there is a small typo in the sample that breaks sorting: the check should be for col index 2 instead of 3:

        Private Sub flex_BeforeSort(sender As Object, e As SortColEventArgs) Handles C1FlexGrid1.BeforeSort
        If e.Col = 2 Then
          C1FlexGrid1.Sort(New ExampleComparer(C1FlexGrid1, e.Order, e.Col))
          e.Handled = True
        End If
      End Sub
    

    After this change, it worked.

    Best regards

    Wolfgang

  • Posted 22 February 2022, 4:42 am EST - Updated 3 October 2022, 10:39 pm EST

    Hello Wolfgang,

    Actually I want to sort using the iComparer on the column 3 (not alphabetical order)

    If you set to col index 2, it sort using the alphabetical order.

  • Posted 22 February 2022, 5:54 am EST

    Oh, my bad - I had converted the sample to a version without data binding, and had missed the fixed column. Thus I had a different column index.

    The doc for C1FlexGrid.Sort says:

    Custom sorting can only be used when the grid is in unbound mode.

    So I assume it is not actually supported. Attached sample creates an unbound grid, and here sorting works as expected.

    GroupPanelUpdate_FG_new.zip

    For sorting the DataTable, you would probably have to set “DataTable.DefaultValue.Sort” to a column index (similar to the “order by” part of a SQL “select” statement). But this is probably not possible in your “real world” app?

    Best regards

    Wolfgang

  • Posted 22 February 2022, 8:46 am EST

    I’ve not seen that in the documentation:

    https://www.grapecity.com/componentone/docs/win/online-flexgrid/sort-operations.html#6

    Is it mentioned elsewhere?

    I could add a column with an “index” but I’m afraid that’ll have side effects.

  • Posted 22 February 2022, 9:01 am EST

    Hi Florent,

    The Custom sorting can only be used when the grid is in Unbound mode, as mentioned in the documentation comments of the C1FlexGrid.Sort method.

    Documentation: https://www.grapecity.com/componentone/docs/win/online-flexgrid/C1.Win.C1FlexGrid.4.5.2~C1.Win.C1FlexGrid.C1FlexGridBase~Sort(IComparer).html

    In Bound mode you will need to sort the data source itself, since DataTable/DataView does not support custom sorting, you will need to sort the data rows and create a new sorted data view that you can then assign to the grid.

    Regards.

    Avnish

    GroupPanelUpdate_FG_mod.zip

  • Posted 22 February 2022, 10:32 am EST

    Hi Avnish,

    Thanks for your updated sample.

    I’ve played with it and it seems when grouping the sort is lost.

    Regards,

    Florent

  • Posted 22 February 2022, 10:33 am EST

    Another question: is it possible to sort according to the userdata? (in bound mode).

  • Posted 23 February 2022, 7:06 am EST

    Hi,

    To Custom sort the column while grouping, you can copy the GroupDescriptions in a list and then Clear the GroupDescriptions before sorting. Then after the sorting is done, you can apply the GroupDescriptions again.

    Since moving rows is not allowed in bound mode, It is not directly possible to sort the Grid on the basis of UserData while in bound mode. As a workaround, you can add a custom column in your DataTable and hide that column from the Grid. Then, you can add your UserData in this column and use values from this column while comparing the Rows in the Custom sort.

    Kindly refer to the updated sample. In the sample, sorting the position column sorts on the basis of hidden User Data column. [GroupPanelUpdate_FG_UserDataSort.zip]

    Regards.

    Avnish

    GroupPanelUpdate_FG_UserDataSort.zip

Need extra support?

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

Learn More

Forum Channels