Posted 26 June 2019, 2:23 pm EST - Updated 4 October 2022, 1:06 am EST
Hi
I have a flexgrid bound to a datasource
Is there a way to implement a custom sort to one of the columns
Thanks
Forums Home / ComponentOne / WinForms Edition
Posted by: Ranba790 on 26 June 2019, 2:23 pm EST
Posted 26 June 2019, 2:23 pm EST - Updated 4 October 2022, 1:06 am EST
Hi
I have a flexgrid bound to a datasource
Is there a way to implement a custom sort to one of the columns
Thanks
Posted 27 June 2019, 6:28 am EST
Hi,
To implement custom sort in bound FlexGrid, you need to create your own Comparer class (implementing IComparer) and use this to sort FlexGrid’s datasource inside FlexGrid’s BeforeSort event handler.
private void C1FlexGrid1_BeforeSort(object sender, C1.Win.C1FlexGrid.SortColEventArgs e) { if(e.Col==1) //the column to have custom sort { e.Cancel = true; List<Room> list = c1FlexGrid1.DataSource as List<Room>; list.Sort(new CustomObjectComparer(e.Col, sortAsc)); c1FlexGrid1.DataSource = list.ToList(); customSort = true; sortAsc = !sortAsc; } }
In addition to this, since we are canceling default sort and implementing our own, we need to also show sort icons ourselves. This can be down through OwnerDrawCell event```
Best wishes,
Ruchir
[zip filename="CustomSort_FlexGrid.zip"]https://gccontent.blob.core.windows.net/forum-uploads/file-9dfc9bf2-fa38-4363-b305-a551a6663ab4.zip[/zip]