原創|其它|編輯:郝浩|2012-09-26 11:30:36.000|閱讀 746 次
概述:有時候客戶想要在一個域內對ASPxGridView的欄進行分組,而在另一個域內對其排序。本文中,我想描述一下如何通過不同的方法實現這個特點。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
有時候客戶想要在一個域內對的欄進行分組,而在另一個域內對其排序。本文中,我想描述一下如何通過不同的方法實現這個特點。因為,這并不像它可能看起來的那樣簡單。
在設計時,我們創建了一個簡單的網格,它的數據從數據源控件處獲得。我們想按城市來對ASPxGridView進行分組,而按國家對它進行排序。
這個任務可以通過以下幾種方法來解決:
1.通過自定義欄位排序:按城市將網格分組,但是通過自定義欄位排序事件按國家對其排序。
2.在另一個域內對網格排序:按城市將網格分組,但是通過分組匯總排序信息類按國家對其排序。
3.通過自定義匯總類型對網格排序:按城市將網格分組,但是通過分組匯總排序信息類和自定義匯總計算事件按國家對其排序。
4.通過它們的名字對值進行排序:使用GridViewDataComboBoxColumn文本完成分組。
下面,你可以找到一些關于這些任務的更詳細的描述:
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>
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:DevExpress中文網