TrueDBGrid - Sort by aggregated value

Posted by: misel on 13 July 2020, 6:03 am EST

    • Post Options:
    • Link

    Posted 13 July 2020, 6:03 am EST

    Dear All,

    i have a simple grid with 3 columns Order Number, Company, Amount. I have the group by panel enabled and the column Amount has an aggregate sum enabled. When I drag the Company column into the Group BY panel, I want to sort the summarized column of Amount by the sum value descending (so I am having the company that ordered the most on the top) How do I do that?

  • Posted 14 July 2020, 7:19 am EST

    Hi Misel,

    The feature itself cannot be added to any grid (FlexGrid, TrueDBGrid, even in MS DataGridView if MS agreed to do such request, etc.) directly when the grid is bound to DataTable, because it requires specific advanced groups sorting based on aggregate values.

    In bound mode the grid displays the inner organization of data in datasource - if the datasource supports sorting (e.g. DataTable, or more precisely - corresponding DataView of DataTable) then grid displays sorted data of that datasource.

    The DataTable/DataView doesn’t provide any built-in mechanism to implement such sort - there is no method in which we can pass some custom IComparer or Comparison between data rows.

    But there are still options to implement such sorting - by creating custom datasource which supports such sorting (but such datasource will be hard to fill from Database using DB adapters) or by creating some columns in DataTable which will have associated aggregate values in it and sorting by that columns firstly.

    Such columns can be hidden in the grid control.

    So, the key points of the solution:

    1. Handle mouse click of the column header programmatically in the corresponding event.
    2. Generate column in datasource which will have corresponding aggregate values and fill that column.
    3. Sort by that column then sort by grouped columns, then sort by column with sorting value itself.
    4. Hide associated aggregate columns in the grid.

    I’d assume that such sorting (on datasource) can be implemented in any grid - FlexGrid, TrueDBGrid, DataGridView if such a grid have a mechanism to cancel sorting in some corresponding event.

    I have attached a sample GroupingDemo_Solution.zip implementing the same, please have a look.

    Regards,

    Prabhat Sharma.

    GroupingDemo_Solution.zip

  • Posted 15 July 2020, 2:26 am EST

    Thank you very much.

    Best Regards,

    Misel

Need extra support?

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

Learn More

Forum Channels