Current filter:
                                You should refresh the page.
                                Support Center
                                0
                                  • Sometimes customers want to group the ASPxGridView's columns by one field, but sort it by another one.

                                    In this article, I want to describe how to implement this feature in various ways, because it is not as easy as it might seem.

                                You must  log in  or  register  to leave comments

                                1 Solution

                                1

                                We have a simple grid created at design time whose data is obtained from the AccessDataSource. We want to group ASPxGridView by cities, but sort it by countries.

                                This task can be solved in the following ways:

                                1. Via custom column sorting: group the grid by cities, but sort it by countries via the CustomColumnSort event;
                                2. Sort the grid by another field: group it by cities, but sort it by countries via the GroupSummarySortInfo class;
                                3. Sort the grid by a custom summary type: group it by cities, but sort it by countries via the GroupSummarySortInfo class and CustomSummaryCalculate event;
                                4. Sort values by their names: perform grouping by using the GridViewDataComboBoxColumn text.

                                Below, you can find a more detailed description of these tasks.

                                1. Custom column sorting: group ASPxGridView by cities, but sort it by countries via the CustomColumnSort event.

                                To utilize this approach, you should handle the ASPxGridView.CustomColumnSort event. A column's Settings.SortMode property specifies how the grid's data is sorted when sorting is applied to this column. In our case, this property is set to 'Custom'. So, a custom sorting algorithm for the "City" column will be implemented by handling the CustomColumnSort event.

                                [ASPx]
                                <dx:GridViewDataTextColumn FieldName="City" VisibleIndex="2" GroupIndex="0"> <Settings SortMode="Custom"/> </dx:GridViewDataTextColumn>

                                In the CustomColumnSort event handler, we compare two rows. The column being processed is specified by the CustomColumnSortEventArgs.Column parameter. The CustomColumnSortEventArgs.Value1 and CustomColumnSortEventArgs.Value2 parameters identify row values within this column.

                                The result of the custom comparison should be set to the CustomColumnSortEventArgs.Result parameter as shown below:

                                   - set Result to -1 if the first row should be positioned above the second row when data is sorted in the ascending order. When the data is sorted in the descending order, the first row will be positioned below the second row.
                                   - set Result to 1 if the first row should be positioned below the second row when the data is sorted in the ascending order. When the data is sorted in the escending order, the first row will be positioned above the second row.
                                   - set Result to 0 to indicate that the rows are equal. In this case, the rows will be arranged within the grid according to their indexes in the data source.

                                We set the e.Handled parameter to true to ignore the default comparison mechanism.

                                [C#]
                                protected void gridCustomers_CustomColumnSort(object sender, CustomColumnSortEventArgs e) { if (e.Column != null & e.Column.FieldName == "City") { object country1 = e.GetRow1Value("Country"); object country2 = e.GetRow2Value("Country"); int res = Comparer.Default.Compare(country1, country2); if (res == 0) { object city1 = e.Value1; object city2 = e.Value2; res = Comparer.Default.Compare(city1, city2); } e.Result = res; e.Handled = true; } }

                                2. Sort ASPxGridView by another field: group ASPxGridView by cities, but sort it by countries via the GroupSummarySortInfo class.

                                Group rows should be sorted by summary values based upon information provided by the ASPxGroupSummarySortInfo objects. Properties introduced by these objects represent the sort order, a summary item used to calculate summary values, etc. These properties are read-only and initialized by the constructor.

                                To sort group rows by summary values, create a new ASPxGroupSummarySortInfo object and add it to the ASPxGridView's GroupSummarySortInfo collection using the GroupSummarySortInfoCollection.Add method. After the ASPxGroupSummarySortInfo object has been added to the collection, group rows are automatically sorted by their summary values.

                                [C#]
                                gridCustomers.GroupSummarySortInfo.Clear(); ASPxGroupSummarySortInfo sortInfo = new ASPxGroupSummarySortInfo(); sortInfo.SortOrder = ColumnSortOrder.Ascending; sortInfo.SummaryItem = gridCustomers.GroupSummary["Country", SummaryItemType.Min]; sortInfo.GroupColumn = "City"; gridCustomers.GroupSummarySortInfo.AddRange(sortInfo);

                                3. Sort ASPxGridView by a custom summary type: group ASPxGridView by cities, but sort it by countries via the GroupSummarySortInfo class and CustomSummaryCalculate event.

                                This solution differs from the previous one only in that the sorting mechanism uses a custom summary value. Summary custom calculation rules are applied in the CustomSummaryCalculate event handler. The CustomSummaryCalculate event fires for each row involved in the summary calculation. When calculating the total summary value, the event is raised for each data row. In the current example, for simplicity, the custom total value is equal to the cell value.

                                Please refer to the ASPxGridView.CustomSummaryCalculate Event and Custom Aggregate Functions help topics to learn more about the custom summary calculation process.

                                [C#]
                                gridCustomers.GroupSummarySortInfo.Clear(); ASPxGroupSummarySortInfo sortInfo = new ASPxGroupSummarySortInfo(); sortInfo.SortOrder = ColumnSortOrder.Ascending; sortInfo.SummaryItem = gridCustomers.GroupSummary["Country", SummaryItemType.Custom]; sortInfo.GroupColumn = "City"; gridCustomers.GroupSummarySortInfo.AddRange(sortInfo);

                                [C#]
                                protected void gridCustomers_CustomSummaryCalculate(object sender, CustomSummaryEventArgs e) { ASPxSummaryItem item = e.Item as ASPxSummaryItem; if (item.FieldName == "Country") { if (e.SummaryProcess == CustomSummaryProcess.Finalize) e.TotalValue = e.FieldValue.ToString(); } }

                                4. Sort values by their names: perform grouping by using the GridViewDataComboBoxColumn text.

                                To sort GridViewDataComboBoxColumn by text rather than by value, you should set the ASPxGridViewBehaviorSettings.SortMode property to 'DisplayText'.

                                [ASPx]
                                <SettingsBehavior SortMode="DisplayText" />

                                Also, this solution demonstrates how to show a custom text for the grouped column. You should use the GroupRowContent template for this purpose:

                                [ASPx]
                                <Templates> <GroupRowContent> <%# "Category: " + Container.GroupText%> </GroupRowContent> </Templates>

                                You must  log in  or  register  to leave comments

                                If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

                                FOLLOW US

                                DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, Silverlight, ASP.NET, WinForms, HTML5 or Windows 8, DevExpress tools help you build and deliver your best in the shortest time possible.

                                Copyright © 1998-2014 Developer Express Inc.
                                All trademarks or registered trademarks are property of their respective owners