As shown in the attachment, I have a gridcontrol grouped by Name. If I sort this gridcontrol by height, only items within a group will be sorted.
What I want is, if I sort height, then, groups will be also sorted by height. The height of each first item of the group will be used to sort.
In another word, after I click Height column, the result that I want is:
Name: d ....... Height 18
Name: a ....... Height 19
....... Height 20
Name: b ....... Height 20
Name: e ....... Height 22
Yes, I know that I can code it by myself, I am just curious that if Devexpress has such an option since I need make a couple of columns like this.
Thank you for the question. The current version of XtraGrid doesn't provide a direct way to achieve this goal. Moreover, this is a rather difficult task to implement, and the requested functionality may cause serious performance problems in your application. Let me explain why. To properly group data, the grid should first sort rows against grouped column values. Sorting should be performed based upon two fields - the Name and the Height in your case. Moreover, data should be sorted using a very specific algorithm, so, rows that have the same name should be arranged one after another, but the group of rows should be arranged based upon the Height field value. Finally, there may be some ambiguity with this approach. For example, if a group with name "a" contains rows with the following heights 15, 25 and the group whose name is "b" contains a row whose heigh is between the above mentioned values - 20 for example. With this data the sorting algorithm you described doesn't make sense. That is why we decided not to introduce this approach. In any case, you can implement the requested solution yourself by handling the CustomColumnSort and CustomColumnGroup events. Please refer to the corresponding documentation to learn more on how to accomplish this goal.
thanks a lot for your detailed response!
Well, then, to my understanding, I should handle CustomColumnSort event.
But just now, I tried to do it. First, I want to see if I can totally disable default sorting. I mean, I still want to show users the sort function. But when users click Sort function, the selected column does not sort at all.
However, somehow, in my example, I set the FirstName column to be custom sort. But only in the first time when I click Sort the First Name, this column doe not sort. But if I click Sort this column again, it will still sort.
May I know if there is something wrong with my code?
thanks a lot!
I'm afraid that I can't reproduce the problem. If I click the FirstName column for the first time, there's an arrow indicating that the column is sorted in the ascending order. The order of rows isn't changed, because the comparison result is always set to zero, which means that compared rows are equal. If I set the SortOrder to descending, the GridView inverts the order of rows.
Sorry, I am still working on this issue.
Just now, I tried to override gridView1_CustomColumnSort(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnSortEventArgs e) method, but I found that if the grid has groups, then, within this method, I can only compare rows within the same group. In another word, in this method, I can never compare rows of different groups?
May I have some hints from you?
Sure, I can also create a "Sort" button and then create my own sorting mechanism, when I click "Sort" button, I will sort all rows and then refresh the entire table. But I am just wondering if there is a better way in GridView?
Sorry for my misunderstanding. I understand what you need to achieve. Unfortunately, this can't be implemented via custom sorting, because the GridView will group data incorrectly in this situation (under certain circumstances, groups will be broken into several parts). The only way to implement this functionality is to create GridGroupSummaryItems, and sort group rows by summary. Attached is a sample project, demonstrating how to implement this.
1. Use the Min summary type, to sort by first values sorted in the ascending order.
2. You can prevent summaries from being displayed in the group row by modifying the GridView.GroupFormat property.
3. Use the GridView.GroupSummarySortInfo property to apply sorting.
Please let me know if you need any further assistance.