方法一:
效果截图,第一行是通过代码添加的,
第二行GridView本身的表头
在GridView的RowCreated事件中写添加代码
-
protected void SmartGridView1_RowCreated(object sender, GridViewRowEventArgs e)
-
{
-
if (e.Row.RowType == DataControlRowType.Header)
-
{
-
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);//表头行
-
TableHeaderCell cell = new TableHeaderCell();
-
cell.ColumnSpan = 5;
-
rowHeader.Cells.Add(cell);
-
cell = new TableHeaderCell();
-
cell.Text = Resources.Coding.Revenue;
-
cell.ColumnSpan = 5;
-
rowHeader.Cells.Add(cell);
-
cell = new TableHeaderCell();
-
cell.Text = Resources.Coding.Cost;
-
cell.ColumnSpan = 5;
-
rowHeader.Cells.Add(cell);
-
((GridView)sender).Controls[0].Controls.AddAt(0, rowHeader);//加到最前面
-
}
-
}
方法二:
实现方法就是给单元格填充我们想要的格式代码。
在行创建事件中加如下代码:
protected void GridView1_RowCreated( object sender, GridViewRowEventArgs e )
{
if (e.Row.RowType == DataControlRowType.Header)
{
//创建一个GridViewRow,相当于表格的 TR 一行
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
string HeaderBackColor = "#EDEDED";
rowHeader.BackColor = System.Drawing.ColorTranslator.FromHtml(HeaderBackColor);
//实现确定要显示的表头样式,也可以通过计算生成
// <tr>
// <td rowspan='2'>关键单元格</td>
// <td colspan='2'>表头文字</td>
// <td colspan='2'>表头文字</td>
// <td>表头文字</td>
// </tr>
// <tr bgcolor='#FFF'>
// <td colspan='2'>表头文字</td>
// <td rowspan='2'>表头文字</td>
// <td colspan='2'>表头文字</td>
// </tr>
// <tr bgcolor='#FFF'>
// <td>表头文字</td>
// <td>表头文字</td>
// <td>表头文字</td>
// <td>表头文字</td>
// <td>表头文字</td>";
// </tr>
// 上面的样式可以设置斜线
Literal newCells = new Literal();
newCells.Text = @"表头文字1</th>
<th colspan='2'>表头文字2</th>
<th colspan='2'>表头文字3</th>
<th>表头文字4</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th colspan='2'>表头文字5</th>
<th rowspan='2'>表头文字6</th>
<th colspan='2'>表头文字7</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th>表头文字8</th>
<th>表头文字9</th>
<th>表头文字10</th>
<th>表头文字11</th>
<th>表头文字12";
TableCellCollection cells = e.Row.Cells;
TableHeaderCell headerCell = new TableHeaderCell();
//下面的属性设置与 <td rowspan='2'>关键单元格</td> 要一致
headerCell.RowSpan = 2;
headerCell.Controls.Add(newCells);
rowHeader.Cells.Add(headerCell);
rowHeader.Cells.Add(headerCell);
rowHeader.Visible = true;
//添加到 GridView1
GridView1.Controls[0].Controls.AddAt(0, rowHeader);
}
}
{
if (e.Row.RowType == DataControlRowType.Header)
{
//创建一个GridViewRow,相当于表格的 TR 一行
GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
string HeaderBackColor = "#EDEDED";
rowHeader.BackColor = System.Drawing.ColorTranslator.FromHtml(HeaderBackColor);
//实现确定要显示的表头样式,也可以通过计算生成
// <tr>
// <td rowspan='2'>关键单元格</td>
// <td colspan='2'>表头文字</td>
// <td colspan='2'>表头文字</td>
// <td>表头文字</td>
// </tr>
// <tr bgcolor='#FFF'>
// <td colspan='2'>表头文字</td>
// <td rowspan='2'>表头文字</td>
// <td colspan='2'>表头文字</td>
// </tr>
// <tr bgcolor='#FFF'>
// <td>表头文字</td>
// <td>表头文字</td>
// <td>表头文字</td>
// <td>表头文字</td>
// <td>表头文字</td>";
// </tr>
// 上面的样式可以设置斜线
Literal newCells = new Literal();
newCells.Text = @"表头文字1</th>
<th colspan='2'>表头文字2</th>
<th colspan='2'>表头文字3</th>
<th>表头文字4</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th colspan='2'>表头文字5</th>
<th rowspan='2'>表头文字6</th>
<th colspan='2'>表头文字7</th>
</tr>
<tr bgcolor='" + HeaderBackColor + "'>";
newCells.Text += @"
<th>表头文字8</th>
<th>表头文字9</th>
<th>表头文字10</th>
<th>表头文字11</th>
<th>表头文字12";
TableCellCollection cells = e.Row.Cells;
TableHeaderCell headerCell = new TableHeaderCell();
//下面的属性设置与 <td rowspan='2'>关键单元格</td> 要一致
headerCell.RowSpan = 2;
headerCell.Controls.Add(newCells);
rowHeader.Cells.Add(headerCell);
rowHeader.Cells.Add(headerCell);
rowHeader.Visible = true;
//添加到 GridView1
GridView1.Controls[0].Controls.AddAt(0, rowHeader);
}
}
方法三(此方法用的一个类文件即可):
表头定义说明:
表头定义方法:相邻父列头之间用''#''分隔,上级行与下级行用空格('' '')分隔,相邻未级子列头用逗号分隔('','').
表头定义方法:相邻父列头之间用''#''分隔,上级行与下级行用空格('' '')分隔,相邻未级子列头用逗号分隔('','').
表头定义示例:
A.两层
A.两层
烟叶等级#级别#保山市 保山,小计#楚雄州 姚安,小计#大理州 宾川,大理,小计#红河州 建水,泸西,弥勒,石屏,小计#昆明市 富民,禄劝,小计#丽江市 华坪,宁蒗,小计#临沧市 沧源,凤庆,耿马,临翔,双江,永德,云县,镇康,小计#普洱市 景东,思茅,镇沅,小计#曲靖市 富源,陆良,罗平,师宗,宣威,小计#文山州 文山,小计#玉溪市 玉溪,小计#总计
B.三层
B.三层
等级#级别#上期结存 件数,重量,比例#本期调入 收购调入 件数,重量,比例#本期发出 车间投料 件数,重量,比例#本期发出 产品外销百分比 件数,重量,比例#平均值
调用说明:使用时在GridView的RowCreated事件中加入下面代码调用
if (e.Row.RowType == DataControlRowType.Header)
{
DynamicTHeaderHepler dHelper = new DynamicTHeaderHepler();
string header = "等级#级别#上期结存 件数,重量,比例#本期调入 收购调入 件数,重量,比例#本期发出 车间投料 件数,重量,"
+ "比例#本期发出 产品外销百分比 件数,重量,比例#平均值";
dHelper.SplitTableHeader(e.Row, header);
}
类文件如下:
//
//***********************************************************************
// Created: 2007-10-29 Author: ruijc
// File: DynamicTHeaderHepler.cs
// Description: 动态生成复合表头帮助类
// 相邻父列头之间用'#'分隔,父列头与子列头用空格(' ')分隔,相邻子列头用逗号分隔(',').
//***********************************************************************
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;
public class DynamicTHeaderHepler
{
public DynamicTHeaderHepler()
{
//
// TODO: Add constructor logic here
//
}
//***********************************************************************
// Created: 2007-10-29 Author: ruijc
// File: DynamicTHeaderHepler.cs
// Description: 动态生成复合表头帮助类
// 相邻父列头之间用'#'分隔,父列头与子列头用空格(' ')分隔,相邻子列头用逗号分隔(',').
//***********************************************************************
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Collections;
public class DynamicTHeaderHepler
{
public DynamicTHeaderHepler()
{
//
// TODO: Add constructor logic here
//
}
/**//// <summary>
/// 重写表头
/// </summary>
/// <param name="targetHeader">目标表头</param>
/// <param name="newHeaderNames">新表头</param>
/// <remarks>
/// 等级#级别#上期结存 件数,重量,比例#本期调入 收购调入 件数,重量,比例#本期发出 车间投料 件数,重量,
/// 比例#本期发出 产品外销百分比 件数,重量,比例#平均值
/// </remarks>
public void SplitTableHeader(GridViewRow targetHeader, string newHeaderNames)
{
TableCellCollection tcl = targetHeader.Cells;//获得表头元素的实例
tcl.Clear();//清除元素
int row = GetRowCount(newHeaderNames);
int col = GetColCount(newHeaderNames);
string[,] nameList = ConvertList(newHeaderNames,row,col);
int RowSpan = 0;
int ColSpan = 0;
for (int k = 0; k < row; k++)
{
string LastFName = "";
for (int i = 0; i < col; i++)
{
if (LastFName == nameList[i, k] && k!=row-1)
{
LastFName = nameList[i, k];
continue;
}
else
{
LastFName = nameList[i, k];
}
int bFlag=IsVisible(nameList, k, i, LastFName);
switch (bFlag)
{
case 0:
break;
case 1:
RowSpan = GetSpanRowCount(nameList,row, k, i);
ColSpan = GetSpanColCount(nameList,row,col, k, i);
tcl.Add(new TableHeaderCell());//添加表头控件
tcl[tcl.Count - 1].RowSpan = RowSpan;
tcl[tcl.Count - 1].ColumnSpan = ColSpan;
tcl[tcl.Count - 1].HorizontalAlign = HorizontalAlign.Center;
tcl[tcl.Count - 1].Text = LastFName;
break;
case -1:
string[] EndColName = LastFName.Split(new char[] { ',' });
foreach(string eName in EndColName){
tcl.Add(new TableHeaderCell());//添加表头控件
tcl[tcl.Count - 1].HorizontalAlign = HorizontalAlign.Center;
tcl[tcl.Count - 1].Text = eName;
}
break;
}
}
if (k != row-1)
{//不是起始行,加入新行标签
tcl[tcl.Count - 1].Text = tcl[tcl.Count - 1].Text+"</th></tr><tr>";
}
}
}
/// 重写表头
/// </summary>
/// <param name="targetHeader">目标表头</param>
/// <param name="newHeaderNames">新表头</param>
/// <remarks>
/// 等级#级别#上期结存 件数,重量,比例#本期调入 收购调入 件数,重量,比例#本期发出 车间投料 件数,重量,
/// 比例#本期发出 产品外销百分比 件数,重量,比例#平均值
/// </remarks>
public void SplitTableHeader(GridViewRow targetHeader, string newHeaderNames)
{
TableCellCollection tcl = targetHeader.Cells;//获得表头元素的实例
tcl.Clear();//清除元素
int row = GetRowCount(newHeaderNames);
int col = GetColCount(newHeaderNames);
string[,] nameList = ConvertList(newHeaderNames,row,col);
int RowSpan = 0;
int ColSpan = 0;
for (int k = 0; k < row; k++)
{
string LastFName = "";
for (int i = 0; i < col; i++)
{
if (LastFName == nameList[i, k] && k!=row-1)
{
LastFName = nameList[i, k];
continue;
}
else
{
LastFName = nameList[i, k];
}
int bFlag=IsVisible(nameList, k, i, LastFName);
switch (bFlag)
{
case 0:
break;
case 1:
RowSpan = GetSpanRowCount(nameList,row, k, i);
ColSpan = GetSpanColCount(nameList,row,col, k, i);
tcl.Add(new TableHeaderCell());//添加表头控件
tcl[tcl.Count - 1].RowSpan = RowSpan;
tcl[tcl.Count - 1].ColumnSpan = ColSpan;
tcl[tcl.Count - 1].HorizontalAlign = HorizontalAlign.Center;
tcl[tcl.Count - 1].Text = LastFName;
break;
case -1:
string[] EndColName = LastFName.Split(new char[] { ',' });
foreach(string eName in EndColName){
tcl.Add(new TableHeaderCell());//添加表头控件
tcl[tcl.Count - 1].HorizontalAlign = HorizontalAlign.Center;
tcl[tcl.Count - 1].Text = eName;
}
break;
}
}
if (k != row-1)
{//不是起始行,加入新行标签
tcl[tcl.Count - 1].Text = tcl[tcl.Count - 1].Text+"</th></tr><tr>";
}
}
}
/**//// <summary>
/// 如果上一行已经输出和当前内容相同的列头,则不显示
/// </summary>
/// <param name="ColumnList">表头集合</param>
/// <param name="rowIndex">行索引</param>
/// <param name="colIndex">列索引</param>
/// <returns>1:显示,-1:含','分隔符,0:不显示</returns>
private int IsVisible(string[,] ColumnList,int rowIndex, int colIndex,string CurrName)
{
if (rowIndex!=0){
if (ColumnList[colIndex,rowIndex-1]==CurrName){
return 0;
}else{
if (ColumnList[colIndex, rowIndex].Contains(","))
{
return -1;
}else{
return 1;
}
}
}
return 1;
}
/// 如果上一行已经输出和当前内容相同的列头,则不显示
/// </summary>
/// <param name="ColumnList">表头集合</param>
/// <param name="rowIndex">行索引</param>
/// <param name="colIndex">列索引</param>
/// <returns>1:显示,-1:含','分隔符,0:不显示</returns>
private int IsVisible(string[,] ColumnList,int rowIndex, int colIndex,string CurrName)
{
if (rowIndex!=0){
if (ColumnList[colIndex,rowIndex-1]==CurrName){
return 0;
}else{
if (ColumnList[colIndex, rowIndex].Contains(","))
{
return -1;
}else{
return 1;
}
}
}
return 1;
}
/**//// <summary>
/// 取得和当前索引行及列对应的下级的内容所跨的行数
/// </summary>
/// <param name="ColumnList">表头集合</param>
/// <param name="row">行数</param>
/// <param name="rowIndex">行索引</param>
/// <param name="colIndex">列索引</param>
/// <returns>行数</returns>
private int GetSpanRowCount(string[,] ColumnList, int row,int rowIndex, int colIndex)
{
string LastName = "";
int RowSpan = 1;
for (int k = rowIndex; k < row; k++)
{
if (ColumnList[colIndex,k]==LastName){
RowSpan++;
}else{
LastName = ColumnList[colIndex, k];
}
}
return RowSpan;
}
/// 取得和当前索引行及列对应的下级的内容所跨的行数
/// </summary>
/// <param name="ColumnList">表头集合</param>
/// <param name="row">行数</param>
/// <param name="rowIndex">行索引</param>
/// <param name="colIndex">列索引</param>
/// <returns>行数</returns>
private int GetSpanRowCount(string[,] ColumnList, int row,int rowIndex, int colIndex)
{
string LastName = "";
int RowSpan = 1;
for (int k = rowIndex; k < row; k++)
{
if (ColumnList[colIndex,k]==LastName){
RowSpan++;
}else{
LastName = ColumnList[colIndex, k];
}
}
return RowSpan;
}
/**//// <summary>
/// 取得和当前索引行及列对应的下级的内容所跨的列数
/// </summary>
/// <param name="ColumnList">表头集合</param>
/// <param name="row">行数</param>
/// <param name="col">列数</param>
/// <param name="rowIndex">行索引</param>
/// <param name="colIndex">列索引</param>
/// <returns>列数</returns>
private int GetSpanColCount(string[,] ColumnList,int row, int col,int rowIndex, int colIndex)
{
string LastName = ColumnList[colIndex, rowIndex] ;
int ColSpan = ColumnList[colIndex, row-1].Split(new char[] { ',' }).Length;
ColSpan = ColSpan == 1 ? 0 : ColSpan;
for(int i=colIndex+1;i<col;i++)
{
if (ColumnList[i, rowIndex] == LastName)
{
ColSpan += ColumnList[i, row - 1].Split(new char[] { ',' }).Length;
}
else
{
LastName = ColumnList[i, rowIndex];
break;
}
}
return ColSpan;
}
/// 取得和当前索引行及列对应的下级的内容所跨的列数
/// </summary>
/// <param name="ColumnList">表头集合</param>
/// <param name="row">行数</param>
/// <param name="col">列数</param>
/// <param name="rowIndex">行索引</param>
/// <param name="colIndex">列索引</param>
/// <returns>列数</returns>
private int GetSpanColCount(string[,] ColumnList,int row, int col,int rowIndex, int colIndex)
{
string LastName = ColumnList[colIndex, rowIndex] ;
int ColSpan = ColumnList[colIndex, row-1].Split(new char[] { ',' }).Length;
ColSpan = ColSpan == 1 ? 0 : ColSpan;
for(int i=colIndex+1;i<col;i++)
{
if (ColumnList[i, rowIndex] == LastName)
{
ColSpan += ColumnList[i, row - 1].Split(new char[] { ',' }).Length;
}
else
{
LastName = ColumnList[i, rowIndex];
break;
}
}
return ColSpan;
}
/**//// <summary>
/// 将已定义的表头保存到数组
/// </summary>
/// <param name="newHeaders">新表头</param>
/// <param name="row">行数</param>
/// <param name="col">列数</param>
/// <returns>表头数组</returns>
private string[,] ConvertList(string newHeaders, int row, int col)
{
string[] ColumnNames = newHeaders.Split(new char[] {'#'});
string[,] news = new string[col, row];
string Name = "";
for (int i = 0; i < col; i++)
{
string[] CurrColNames = ColumnNames[i].ToString().Split(new char[] { ' ' });
for (int k = 0; k < row; k++)
{
if (CurrColNames.Length - 1 >= k)
{
if (CurrColNames[k].Contains(","))
{
if (CurrColNames.Length != row)
{
if (Name == "")
{
news[i, k] = news[i, k - 1];
Name = CurrColNames[k].ToString();
}
else
{
news[i, k + 1] = Name;
Name = "";
}
}else{
news[i, k] = CurrColNames[k].ToString();
}
}else{
news[i, k] = CurrColNames[k].ToString();
}
}else{
if (Name == "")
{
news[i, k] = news[i, k - 1];
}else{
/// 将已定义的表头保存到数组
/// </summary>
/// <param name="newHeaders">新表头</param>
/// <param name="row">行数</param>
/// <param name="col">列数</param>
/// <returns>表头数组</returns>
private string[,] ConvertList(string newHeaders, int row, int col)
{
string[] ColumnNames = newHeaders.Split(new char[] {'#'});
string[,] news = new string[col, row];
string Name = "";
for (int i = 0; i < col; i++)
{
string[] CurrColNames = ColumnNames[i].ToString().Split(new char[] { ' ' });
for (int k = 0; k < row; k++)
{
if (CurrColNames.Length - 1 >= k)
{
if (CurrColNames[k].Contains(","))
{
if (CurrColNames.Length != row)
{
if (Name == "")
{
news[i, k] = news[i, k - 1];
Name = CurrColNames[k].ToString();
}
else
{
news[i, k + 1] = Name;
Name = "";
}
}else{
news[i, k] = CurrColNames[k].ToString();
}
}else{
news[i, k] = CurrColNames[k].ToString();
}
}else{
if (Name == "")
{
news[i, k] = news[i, k - 1];
}else{
news[i, k] = Name;
Name = "";
}
}
}
}
return news;
}
Name = "";
}
}
}
}
return news;
}
/**//// <summary>
/// 取得复合表头的行数
/// </summary>
/// <param name="newHeaders">新表头</param>
/// <returns>行数</returns>
private int GetRowCount(string newHeaders)
{
string[] ColumnNames = newHeaders.Split(new char[] { '#' });
int Count = 0;
foreach(string name in ColumnNames){
int TempCount = name.Split(new char[] { ' ' }).Length;
if (TempCount > Count)
Count = TempCount;
}
return Count;
}
/// 取得复合表头的行数
/// </summary>
/// <param name="newHeaders">新表头</param>
/// <returns>行数</returns>
private int GetRowCount(string newHeaders)
{
string[] ColumnNames = newHeaders.Split(new char[] { '#' });
int Count = 0;
foreach(string name in ColumnNames){
int TempCount = name.Split(new char[] { ' ' }).Length;
if (TempCount > Count)
Count = TempCount;
}
return Count;
}
/**//// <summary>
/// 取得复合表头的列数
/// </summary>
/// <param name="newHeaders">新表头</param>
/// <returns>列数</returns>
private int GetColCount(string newHeaders)
{
return newHeaders.Split(new char[] { '#' }).Length;
}
}
/// 取得复合表头的列数
/// </summary>
/// <param name="newHeaders">新表头</param>
/// <returns>列数</returns>
private int GetColCount(string newHeaders)
{
return newHeaders.Split(new char[] { '#' }).Length;
}
}