如何实现ASPxGridView的多表头功能

本文介绍了一种实现ASPxGridView多表头的方法,通过自定义代码可以在数据绑定前添加额外的表头行,支持字段按指定字符进行合并。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
如何实现ASPxGridView的多表头功能

   关于如何实现ASPxGridView的多表头功能,写了一个公共方法可供使用,调用时只许在绑定方法前面调用即可。

代码如下:

//调用该方法即可 public static void SetAspxGridViewBand(DevExpress.Web.ASPxGridView.ASPxGridView gridView) { gridView.HtmlRowCreated += new DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventHandler(GridView1_HtmlRowCreated); } static void GridView1_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableRowEventArgs e) { DevExpress.Web.ASPxGridView.ASPxGridView gridView = sender as DevExpress.Web.ASPxGridView.ASPxGridView; ////if the first data row has been added if (e.RowType == DevExpress.Web.ASPxGridView.GridViewRowType.Data && e.VisibleIndex == gridView.PageIndex * gridView.SettingsPager.PageSize) { Table table = e.Row.Parent as Table; if (table != null) { TableRow row = new TableRow(); TableCell firstBand = GetNewTableCell(row, string.Empty); foreach (DevExpress.Web.ASPxGridView.GridViewDataColumn col in gridView.Columns) { if (col.FieldName.IndexOf("|") > 0) { string bandname = col.FieldName.Substring(0, col.FieldName.IndexOf("|")); TableCell band = null; foreach (TableCell t in row.Cells) { if (t.Text == bandname) { band = t; break; } } if (band == null) { band = GetNewTableCell(row, bandname); } col.Caption = col.FieldName.Substring(col.FieldName.IndexOf("|") + 1); band.ColumnSpan++; } else { firstBand.ColumnSpan++; } } table.Rows.AddAt(0, row); } } } //可以在此修改单元格样式 private static TableCell GetNewTableCell(TableRow row, string bandname) { TableCell band = new TableCell(); band.Text = bandname; row.Cells.Add(band); band.CssClass = "dxgvHeader"; band.HorizontalAlign = HorizontalAlign.Center; band.BorderStyle = BorderStyle.NotSet; band.BorderColor = row.BorderColor; return band; }

注意:

1、该方法在绑定方法之前调用。

2、需合并的单元格的 列需如下设置:

 
<dx:GridViewDataTextColumn Caption="合计|重号" VisibleIndex="0" FieldName="合计|重号">
                               </dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="合计|缺号" VisibleIndex="0" FieldName="合计|缺号">
                               </dx:GridViewDataTextColumn>
<dx:GridViewDataTextColumn Caption="合计|错号" VisibleIndex="0" FieldName="合计|错号"><BR>                                </dx:GridViewDataTextColumn>

 
按照“|”分割判断,相同的则会总动合并。<BR><BR><BR>
 
<BR>
 
<BR>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值