关于如何实现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="合计|错号">
</dx:GridViewDataTextColumn>
按照“|”分割判断,相同的则会总动合并。