翻譯|使用教程|編輯:龔雪|2025-07-29 10:20:38.413|閱讀 94 次
概述:本教程主要為大家介紹DevExpress WinForms數據網格控件中的分組行API,歡迎下載最新版組件體驗!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
DevExpress WinForms擁有180+組件和UI庫,能為Windows Forms平臺創建具有影響力的業務解決方案。DevExpress WinForms能完美構建流暢、美觀且易于使用的應用程序,無論是Office風格的界面,還是分析處理大批量的業務數據,它都能輕松勝任!
本教程將使用DevExpress WinForms數據網格控件中的分組行API來自定義網格操作,將創建一個示例應用程序,其中:
獲取DevExpress WinForms v25.1正式版下載
DevExpress技術交流群11:749942875 歡迎一起進群討論
要學習識別和遍歷組行的基礎知識,請從一個具有GridControl的應用程序開始,該應用程序已經應用了分組。
首先在狀態欄中顯示焦點行句柄,選擇網格視圖并處理它的事件,該事件在焦點在行之間移動時引發,當前聚焦的行由事件的參數指定,一個單獨的方法用于在狀態欄中顯示信息。
C#
private void DisplayRowHandle(int rowHandle) { // Display the focused row handle. siRowHandleInfo.Caption = "Focused Row Handle: " + rowHandle.ToString(); } private void gridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) { int focusedRowHandle = e.FocusedRowHandle; DisplayRowHandle(focusedRowHandle); }
運行應用程序。由于最上面的組行現在是集中的,狀態欄顯示其句柄等于-1。關注下面的組行,看看它的句柄是-2。如您所見,組行句柄是以-1開頭的負整數。請注意,行從上到下編號,而不考慮它們的嵌套級別。
數據行句柄是以0開頭的非負整數,第一個數據行的句柄是0,然后是1,依此類推。
現在修改網格的功能,以便根級組行在聚焦時自動展開。向事件處理程序再添加一個方法調用,在該方法中,使用方法檢查焦點行是否為根組行。對于顯示在最高層次結構級別的組行,該方法返回0。之后,通過調用方法展開焦點組行,將以下參數傳遞給此方法:聚焦行句柄,true用于展開組行,true用于在所有嵌套級別上額外展開子組行。
C#
private void ExpandFirstLevelGroupRow(int rowHandle) { // Check whether the specified row is a first-level group row. if (gridView.GetRowLevel(rowHandle) == 0) // Expand the row. gridView.SetRowExpanded(rowHandle, true, true); } private void gridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) { // ... ExpandFirstLevelGroupRow(focusedRowHandle); }
運行應用程序,聚焦的根組行與它的嵌套組一起自動展開。
現在修改操作,以便每次只展開一個根級組行。向事件處理程序添加一個方法調用 - CollapseFirstLevelGroupRows,該方法從-1開始倒數,迭代所有組行,直到計數器到達無效的行句柄。使用方法將非聚焦組行與其嵌套組行一起折疊,第二個參數為false。
C#
private void CollapseFirstLevelGroupRows(int rowHandleToKeepExpanded) { // Check whether the specified row is a root group row. if (gridView.GetRowLevel(rowHandleToKeepExpanded) == 0) { // Collapse all root group rows except the specified row. int rowHandle = -1; while (gridView.IsValidRowHandle(rowHandle)) { if ((rowHandle != rowHandleToKeepExpanded) && (gridView.GetRowLevel(rowHandle) == 0)) gridView.SetRowExpanded(rowHandle, false, true); rowHandle--; } } } private void gridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) { // ... CollapseFirstLevelGroupRows(focusedRowHandle); }
現在再次運行應用程序,當根組行獲得焦點時,該組與它的嵌套組一起自動展開,而其他組則折疊。因此可以隨時擴展單個根組。注意,最終用戶可以折疊聚焦組行,也有一種方法可以改變這種操作。
關閉應用程序。聲明一個變量,該變量將存儲最后展開的根組行的句柄。處理視圖的 事件,該事件在組行被展開后觸發。使用前面聲明的變量保存當前展開的根組行。之后,處理View的 事件,該事件在最終用戶試圖折疊組行時引發。要防止當前展開的根組行被折疊,請將事件的參數設置為false。
C#
using DevExpress.XtraGrid.Views.Grid; //... int expandedRowHandle; private void gridView_GroupRowExpanded(object sender, DevExpress.XtraGrid.Views.Base.RowEventArgs e) { GridView view = sender as GridView; if (view == null) return; // Save the currently expanded root group row. if (view.GetRowLevel(e.RowHandle) == 0) expandedRowHandle = e.RowHandle; } private void gridView_GroupRowCollapsing(object sender, DevExpress.XtraGrid.Views.Base.RowAllowEventArgs e) { // Prevent the expanded root group row from being collapsed. if (e.RowHandle == expandedRowHandle) e.Allow = false; }
運行應用程序來查看結果,現在無法折疊聚焦的根行組,因此總是展開要給根行組,它的嵌套組行可以具有任何狀態。
現在回到設計時,進一步修改事件處理程序,聚焦組行或數據行時顯示當前組的信息。
創建DisplayParentChildInfo函數,并在事件處理程序中調用它。要確定聚焦行句柄是否引用組行,請使用方法,函數返回特定組行的子節點數。
對于集中的數據行,狀態欄應該在所有級別上顯示其父組行的信息。使用方法向上移動組行層次結構,在每個層次結構級別,使用方法讀取組行中顯示的文本。
C#
private void DisplayParentChildInfo(int rowHandle) { // For a group row, display the number of its children. if (gridView.IsGroupRow(rowHandle)) siParentChildInfo.Caption = "Group child count: " + gridView.GetChildRowCount(rowHandle).ToString(); else { // For a data row, display info on parent group rows. int parentRowHandle = gridView.GetParentRowHandle(rowHandle); string caption = "Group row "; while (gridView.IsValidRowHandle(parentRowHandle)) { caption += "- " + gridView.GetGroupRowDisplayText(parentRowHandle); parentRowHandle = gridView.GetParentRowHandle(parentRowHandle); } siParentChildInfo.Caption = caption; } } private void gridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) { // ... DisplayParentChildInfo(focusedRowHandle); }
運行應用程序并查看結果。當您聚焦組行時,狀態欄將顯示其子行的數量。如果焦點行是數據行,則狀態欄顯示所有級別的父組行信息。
C#
using DevExpress.XtraGrid.Views.Grid; private void DisplayRowHandle(int rowHandle) { // Display the focused row handle. siRowHandleInfo.Caption = "Focused Row Handle: " + rowHandle.ToString(); } private void ExpandFirstLevelGroupRow(int rowHandle) { // Check whether the specified row is a first-level group row. if (gridView.GetRowLevel(rowHandle) == 0) // Expand the row. gridView.SetRowExpanded(rowHandle, true, true); } private void CollapseFirstLevelGroupRows(int rowHandleToKeepExpanded) { // Check whether the specified row is a root group row. if (gridView.GetRowLevel(rowHandleToKeepExpanded) == 0) { // Collapse all root group rows except the specified row. int rowHandle = -1; while (gridView.IsValidRowHandle(rowHandle)) { if ((rowHandle != rowHandleToKeepExpanded) && (gridView.GetRowLevel(rowHandle) == 0)) gridView.SetRowExpanded(rowHandle, false, true); rowHandle--; } } } private void DisplayParentChildInfo(int rowHandle) { // For a group row, display the number of its children. if (gridView.IsGroupRow(rowHandle)) siParentChildInfo.Caption = "Group child count: " + gridView.GetChildRowCount(rowHandle).ToString(); else { // For a data row, display info on parent group rows. int parentRowHandle = gridView.GetParentRowHandle(rowHandle); string caption = "Group row "; while (gridView.IsValidRowHandle(parentRowHandle)) { caption += "- " + gridView.GetGroupRowDisplayText(parentRowHandle); parentRowHandle = gridView.GetParentRowHandle(parentRowHandle); } siParentChildInfo.Caption = caption; } } private void gridView_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e) { int focusedRowHandle = e.FocusedRowHandle; DisplayRowHandle(focusedRowHandle); ExpandFirstLevelGroupRow(focusedRowHandle); CollapseFirstLevelGroupRows(focusedRowHandle); DisplayParentChildInfo(focusedRowHandle); } int expandedRowHandle; private void gridView_GroupRowExpanded(object sender, DevExpress.XtraGrid.Views.Base.RowEventArgs e) { GridView view = sender as GridView; if (view == null) return; // Save the currently expanded root group row. if (view.GetRowLevel(e.RowHandle) == 0) expandedRowHandle = e.RowHandle; } private void gridView_GroupRowCollapsing(object sender, DevExpress.XtraGrid.Views.Base.RowAllowEventArgs e) { // Prevent the expanded root group row from being collapsed. if (e.RowHandle == expandedRowHandle) e.Allow = false; }
慧都是?家?業數字化解決?案公司,專注于軟件、?油與?業領域,以深?的業務理解和?業經驗,幫助企業實現智能化轉型與持續競爭優勢。
慧都科技是DevExpress的中國區的合作伙伴,DevExpress作為用戶界面領域的優秀產品,幫助企業高效構建權限管理、數據可視化(如網格/圖表/儀表盤)、跨平臺系統(WinForms/ASP.NET/.NET MAUI)及行業定制解決方案,加速開發并強化交互體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都網