有時候客戶想要在一個域內對ASPxGridView的欄進行分組,而在另外一個域內對其排序。本文中,我想描述一下如何經過不一樣的方法實現這個特色。由於,這並不像它可能看起來的那樣簡單。html
在設計時,咱們建立了一個簡單的網格,它的數據從數據源控件處得到。咱們想按城市來對ASPxGridView進行分組,而按國家對它進行排序。算法
這個任務能夠經過如下幾種方法來解決:express
1.經過自定義欄位排序:按城市將網格分組,可是經過自定義欄位排序事件按國家對其排序。函數
2.在另外一個域內對網格排序:按城市將網格分組,可是經過分組彙總排序信息類按國家對其排序。學習
3.經過自定義彙總類型對網格排序:按城市將網格分組,可是經過分組彙總排序信息類和自定義彙總計算事件按國家對其排序。設計
4.經過它們的名字對值進行排序:使用GridViewDataComboBoxColumn文本完成分組。htm
下面,你能夠找到一些關於這些任務的更詳細的描述:對象
1.自定義欄位排序:按城市將ASPxGridView分組,可是經過自定義欄位排序事件按國家對其排序。排序
要使用這種方法,你應該運用ASPxGridView.CustomColumnSort 事件。一個欄的Settings.SortMode 屬性指定了當對這個欄進行排序時,網格的數據是如何被排序的。在咱們這個例子中,屬性值被設爲‘自定義’。所以,一個針對‘城市’欄的自定義排序算法將會 經過處理自定義欄位排序事件被執行。索引
[ASPx]
<dx:GridViewDataTextColumn FieldName="City" VisibleIndex="2" GroupIndex="0">
<Settings SortMode="Custom"/>
</dx:GridViewDataTextColumn>
在自定義欄位排序事件句柄中,咱們對兩行進行了比較。經過CustomColumnSortEventArgs.Column 參數來指定被處理的欄位。CustomColumnSortEventArgs.Value1 和CustomColumnSortEventArgs.Value2 兩個參數區分出在這個欄中的行值。
自定義比較的結果用來設定CustomColumnSortEventArgs.Result 的參數,以下所示:
咱們把e.handled參數的值設爲真,從而忽略掉默認的比較機制。
[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.在另外一個域內對ASPxGridView排序:按城市將ASPxGridView分組,可是經過分組彙總排序信息類按國家對其排序。
根據ASPxGroupSummarySortInfo對象提供的信息,基於此信息獲得的彙總值來將組中的行排序。這些對象引入的屬性表明了排序次序,用來計算彙總值的彙總項,等等。這些屬性是隻讀的,而且由構造函數來初始化。
經過彙總值來排序組中的行,建立一個ASPxGroupSummarySortInfo對象,使用 GroupSummarySortInfoCollection.Add 方法把它添加到ASPxGridView的分組彙總排序信息集合裏。把ASPxGroupSummarySortInfo對象添加到這個集合之後,組中的 行就會根據它們的彙總值自動的排序。
[C#]
gridCustomers.GroupSummarySortInfo.Clear();
ASPxGroupSummarySortInfo sortInfo = new ASPxGroupSummarySortInfo();
sortInfo.SortOrder = ColumnSortOrder.Ascending;
sortInfo.SummaryItem = gridCustomers.GroupSummary["Country", SummaryItemType.Min];
sortInfo.GroupColumn = "City";
3.經過自定義彙總類型對ASPxGridView排序:按城市將ASPxGridView分組,可是經過分組彙總排序信息類和自定義彙總計算事件按國家對其排序。
這種方法與前一種方法的不一樣點僅僅在於排序機制使用了一個自定義的彙總值。彙總自定義計算法則應用到自定義彙總計算事件句柄中。自定義彙總計算事件會觸發 彙總計算中涉及到的每一行。當計算總的彙總值的時候,該事件將會被每個數據行觸發。在當前這個例子裏,爲了簡單起見,自定義的總的彙總值等於子彙總值。
請參考ASPxGridView.CustomSummaryCalculate 事件和自定義彙總函數,這有助於學習更多關於自定義彙總計算過程的知識。
[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.經過它們的名字對值進行排序:使用GridViewDataComboBoxColumn文本完成分組。
想經過文本而不是值來對GridViewDataComboBoxColumn進行排序的話,你應該將ASPxGridViewBehaviorSettings.SortMode 屬性值設爲‘顯示文本’。
[ASPx]
<SettingsBehavior SortMode="DisplayText" />
一樣,這種方法演示瞭如何爲已分組的欄顯示一個自定義文本。你應該使用GroupRowContent模板來完成這個任務。
[ASPx]
<Templates>
<GroupRowContent>
<%# "Category: " + Container.GroupText%>
</GroupRowContent>
</Templates>