FlexGrid (bound) DataMap column sort

Posted by: james on 20 October 2024, 11:26 pm EST

    • Post Options:
    • Link

    Posted 20 October 2024, 11:26 pm EST

    We have a FlexGrid that is bound to a bindingsource/datatable, and we have a column that uses a DataMap (IDictionary): flexGridColumnSupplierID.DataMap = GetSupplierDataMap();

    Our issue is that when the user sorts the column, it sorts by the id value of the column not the mapped string. In other words, it sorts by the data the user does not see, and not by the string that they do see.

    I did a bit of a search through the forums etc… and the closest thing I could find to a solution was using an IComparer, but it is only supported for Unbound mode.

    Is there anyway to do this?

    I can think of one method, where would we add the text value as an inner select to the datatable, and then override the sort method to sort that column instead, but it seems like there should be a built in method as it sort of makes sorting not supported properly when using datamaps.

  • Posted 21 October 2024, 2:07 pm EST

    Hello,

    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.

    Please refer to the attached sample for implementation. (see FlexGrid_DataMap.zip)

    Regards,

    Uttkarsh.

  • Posted 21 October 2024, 8:12 pm EST - Updated 21 October 2024, 8:53 pm EST

    Hi Uttkarsh

    Thanks for the sample. It seems like a rather intense process, but I’ll give it a go. I do a have a further question:

    If we have a table

    Col1, Col2, Col3 - where Col2 is a DataMap column, and Col3 is a hidden column with the actual DataMap value (SQL inner select), can we sort by Col3 if the user clicks Col2.

    Hope that makes sense. Basically, we will be putting the DataMap values in a hidden column by way of an inner select in the datatable, but we will be displaying the DataMap column instead because the grid is editable.

    Also, we are using a BindingSource in between, so the FlexGrid is bound to a BindingSource. Does this make a difference?

    Also, column filtering on the flexgrid for the user works as expected - would be nice if sorting could be built in to? Is it possible to get this as a feature request?

    Thanks

  • Posted 22 October 2024, 2:39 am EST

    Shouldn’t it also work by setting the “DefaultView.Sort”? E.g. with this code snippet in “BeforeSort”:

            if (e.Order == SortFlags.Ascending)
            {
              (c1FlexGrid1.DataSource as DataTable).DefaultView.Sort = "DataMapCol_Val";
            }
            else
            {
              (c1FlexGrid1.DataSource as DataTable).DefaultView.Sort = "DataMapCol_Val desc";
            }

    Best regards

    Wolfgang

  • Posted 22 October 2024, 4:43 am EST

    Hello,

    You you can add an additional column in your datasource with the string values of the datamap column and use it to sort the datamap column. It’ll be less complex and clean solution.

    Please refer to the attached sample for the same. (see FlexGrid_DataMap2.zip)


    Thank you, Wolfgang for your input.

    Sorting on the “DataMapCol_Val” column sorts data based on the datamap keys (which is also the default behavior), whereas the requirement is to sort on the datamap values.

    Regards,

    Uttkarsh.

  • Posted 22 October 2024, 4:55 am EST

    Hello James,

    Also, we are using a BindingSource in between, so the FlexGrid is bound to a BindingSource. Does this make a difference?

    It should not make any difference, but if you observe any discrepancies, please let us know.

    Also, column filtering on the flexgrid for the user works as expected - would be nice if sorting could be built in to? Is it possible to get this as a feature request?

    The sorting is done on the DataSource, in your case, on the DataTable. DataTable contains the datamap’s key values, so the sorting is done using the key values.

    In the case of filtering, our filter UI displays the datamap values, but internally it uses the datamap keys for filtering. Also, the filtering is done on the FlexGrid level, unlike sorting, which is done on the DataSource level.

    Regards,

    Uttkarsh.

  • Posted 23 October 2024, 8:37 pm EST

    All makes sense. Thanks guys. I thought the easiest way would be to add an extra column to the datatable that has the display values, but hidden, and sort by that - which appears to be what you are saying is the way to go. I asked just in-case I was missing something, and there was an easier way.

    Thanks for your help and the samples.

  • Posted 23 October 2024, 11:42 pm EST

    Hello James,

    Yes, you are right, adding an additional column indeed is a better solution to your use case.

    You can refer to the following comment for the sample: https://developer.mescius.com/forums/winforms-edition/flexgrid-bound-datamap-column-sort#77549

    If you require any further assistance or have any questions, please let us know.

    Regards,

    Uttkarsh.

Need extra support?

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

Learn More

Forum Channels