DevExpress WinForms中文教程:Grouping(分组)- 分组行API

翻译|使用教程|编辑:龚雪|2025-07-29 10:20:38.413|阅读 12 次

概述:本教程主要为大家介绍DevExpress WinForms数据网格控件中的分组行API,欢迎下载最新版组件体验!

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

相关链接:

DevExpress WinForms拥有180+组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜任!

DevExpress WinForms中文使用教程图集

本教程将使用DevExpress WinForms数据网格控件中的分组行API来自定义网格操作,将创建一个示例应用程序,其中:

  • 网格在根级别上自动展开焦点组行;
  • 最终用户不能折叠这些行;
  • 其他根级组行将自动折叠;
  • 父行或子行数据将自动显示在状态栏中。

获取DevExpress WinForms v25.1正式版下载

DevExpress技术交流群11:749942875      欢迎一起进群讨论

开始

要学习识别和遍历组行的基础知识,请从一个具有GridControl的应用程序开始,该应用程序已经应用了分组。

DevExpress WinForms中文使用教程图集
显示焦点行句柄

首先在状态栏中显示焦点行句柄,选择网格视图并处理它的ColumnView.FocusedRowChanged事件,该事件在焦点在行之间移动时引发,当前聚焦的行由事件的FocusedRowChangedEventArgs.FocusedRowHandle参数指定,一个单独的方法用于在状态栏中显示信息。

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,依此类推。

DevExpress WinForms中文使用教程图集
展开聚焦的根组行

现在修改网格的功能,以便根级组行在聚焦时自动展开。向ColumnView.FocusedRowChanged事件处理程序再添加一个方法调用,在该方法中,使用GridView.GetRowLevel方法检查焦点行是否为根组行。对于显示在最高层次结构级别的组行,该方法返回0。之后,通过调用GridView.SetRowExpanded方法展开焦点组行,将以下参数传递给此方法:聚焦行句柄,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开始倒数,迭代所有组行,直到计数器到达无效的行句柄。使用GridView.SetRowExpanded方法将非聚焦组行与其嵌套组行一起折叠,第二个参数为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);
}

现在再次运行应用程序,当根组行获得焦点时,该组与它的嵌套组一起自动展开,而其他组则折叠。因此可以随时扩展单个根组。注意,最终用户可以折叠聚焦组行,也有一种方法可以改变这种操作。

防止最终用户折叠展开的根组行

关闭应用程序。声明一个变量,该变量将存储最后展开的根组行的句柄。处理视图的GridView.GroupRowExpanded 事件,该事件在组行被展开后触发。使用前面声明的变量保存当前展开的根组行。之后,处理View的GridView.GroupRowCollapsing 事件,该事件在最终用户试图折叠组行时引发。要防止当前展开的根组行被折叠,请将事件的RowAllowEventArgs.Allow参数设置为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;
}

运行应用程序来查看结果,现在无法折叠聚焦的根行组,因此总是展开要给根行组,它的嵌套组行可以具有任何状态。

显示分组信息

现在回到设计时,进一步修改ColumnView.FocusedRowChanged事件处理程序,聚焦组行或数据行时显示当前组的信息。

创建DisplayParentChildInfo函数,并在事件处理程序中调用它。要确定聚焦行句柄是否引用组行,请使用GridView.IsGroupRow方法,GridView.GetChildRowCount函数返回特定组行的子节点数。

对于集中的数据行,状态栏应该在所有级别上显示其父组行的信息。使用GridView.GetParentRowHandle方法向上移动组行层次结构,在每个层次结构级别,使用GridView.GetGroupRowDisplayText方法读取组行中显示的文本。

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);
}

运行应用程序并查看结果。当您聚焦组行时,状态栏将显示其子行的数量。如果焦点行是数据行,则状态栏显示所有级别的父组行信息。

DevExpress WinForms中文使用教程图集
完整代码

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的中国区的合作伙伴,DevExpress作为用户界面领域的优秀产品,帮助企业高效构建权限管理、数据可视化(如网格/图表/仪表盘)、跨平台系统(WinForms/ASP.NET/.NET MAUI)及行业定制解决方案,加速开发并强化交互体验。


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:慧都网

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP