1、建立一个类,
public class MultTitle
{
public MultTitle()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//title 为一个自定义的Titles类数组,其有两个属性,一个为Span:要合并的列数;一个是text:单元格中内容。关于Titles类请自己定义,很容易的。
public
static void createTitle( object sender, GridViewRowEventArgs e,Titles[] title)
... {
YYControls.SmartGridView grid=((YYControls.SmartGridView) sender);
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);//建立一个新行。
TableCellCollection cells = e.Row.Cells;//得到原来的标题行。
rowHeader.CssClass = "userHeaderRows";//标题行的样式
int sumCol = 0;//用于记录原标题行的当前单元格号。
for (int col = 0; col < title.Length; col++)
...{
TableCell headerCell = new TableCell();
headerCell = new TableCell();
headerCell.Text = title[col].text;
headerCell.HorizontalAlign = HorizontalAlign.Center;
headerCell.VerticalAlign = VerticalAlign.Middle;
headerCell.ColumnSpan = title[col].span;//合并左右相邻的单元格
if (title[col].span == 1)//如果列是1列,即不合并单元格,则就要合并上下单元格。
...{
cells.RemoveAt(sumCol);//删除原标题行的与当前单元格对应的单元格。
sumCol = sumCol - 1;//原标题当前单元格减1,因为上面删除了1个单元格。
headerCell.RowSpan = 2;//合并上下个单元格。
}
sumCol = sumCol + title[col].span;//计算原标题行的当前单元格
rowHeader.Cells.Add(headerCell);//增加一个单元格
}
rowHeader.Visible = true;//显示行
grid.Controls[0].Controls.AddAt(0, rowHeader);//增加到原标题行前面
}
}
2 、在GridView中
的RowCreate事件中调用上面的类即可实现GridView的多标题行。