asp.net中GridView中动态多层表头的处理
2009-03-06 15:11
267 查看
只需在GridView的RowCreated事件中加入以下代码,且数据源列头使用|划分表头层次关系,如[收入|A项],[收入|B项],则会自动创建两层,且自动合并[收入]的单元格。
int layerCount = 0;//层数
TableCell[] oldCells;
if (e.Row.RowType == DataControlRowType.Header)
{
oldCells = new TableCell[e.Row.Cells.Count];
e.Row.Cells.CopyTo(oldCells, 0);
//获取最大层数
for (int i = 0; i < e.Row.Cells.Count; i++)
{
int t = oldCells[i].Text.Split('|').Length;
if (t > layerCount)
{
layerCount = t;
}
}
e.Row.Cells.Clear();
TableHeaderCell tc = null;
int flag = 0;
//处理行
for (int i = 1; i <= layerCount; i++)
{
for (int j = 1; j <= oldCells.Length; j++)
{
string[] arr = oldCells[j - 1].Text.Split('|');
if (arr.Length == i)
{
tc = new TableHeaderCell();
if (layerCount - i > 0)
{
tc.RowSpan = layerCount - i + 1;//定义表头的所占的行数
}
tc.Text = arr[i - 1];
tc.CssClass = _gv.HeaderStyle.CssClass;
e.Row.Cells.Add(tc);
}
else if (arr.Length > i)
{
flag++;
if (j >= oldCells.Length || oldCells[j].Text.Split('|').Length < i || arr[i - 1] !=
oldCells[j].Text.Split('|')[i - 1])
{
tc = new TableHeaderCell();
tc.ColumnSpan = flag;
tc.Text = arr[i - 1];
tc.CssClass = _gv.HeaderStyle.CssClass;
e.Row.Cells.Add(tc);
flag = 0;
}
}
}
if (i < layerCount)
{
if (tc != null)
{
tc.Text += "</tr><tr>";
}
}
}
}
实现上图所示的效果则数据源表头格式为:
报修区域,技术难度,当日|维修总费用|合计,当日|维修总费用|材料,当日|维修总费用|管理,当日|修次当日|耗时,当日|状态|合计,当日|状态|已完,当日|状态|未完,本月平均|维修总费用|合计,本月平均|维修总费用|材料,本月平均|维修总费用|管理,本月平均|修次,本月平均|耗时,本月平均|状态|合计,本月平均|状态|已完,本月平均|状态|未完
int layerCount = 0;//层数
TableCell[] oldCells;
if (e.Row.RowType == DataControlRowType.Header)
{
oldCells = new TableCell[e.Row.Cells.Count];
e.Row.Cells.CopyTo(oldCells, 0);
//获取最大层数
for (int i = 0; i < e.Row.Cells.Count; i++)
{
int t = oldCells[i].Text.Split('|').Length;
if (t > layerCount)
{
layerCount = t;
}
}
e.Row.Cells.Clear();
TableHeaderCell tc = null;
int flag = 0;
//处理行
for (int i = 1; i <= layerCount; i++)
{
for (int j = 1; j <= oldCells.Length; j++)
{
string[] arr = oldCells[j - 1].Text.Split('|');
if (arr.Length == i)
{
tc = new TableHeaderCell();
if (layerCount - i > 0)
{
tc.RowSpan = layerCount - i + 1;//定义表头的所占的行数
}
tc.Text = arr[i - 1];
tc.CssClass = _gv.HeaderStyle.CssClass;
e.Row.Cells.Add(tc);
}
else if (arr.Length > i)
{
flag++;
if (j >= oldCells.Length || oldCells[j].Text.Split('|').Length < i || arr[i - 1] !=
oldCells[j].Text.Split('|')[i - 1])
{
tc = new TableHeaderCell();
tc.ColumnSpan = flag;
tc.Text = arr[i - 1];
tc.CssClass = _gv.HeaderStyle.CssClass;
e.Row.Cells.Add(tc);
flag = 0;
}
}
}
if (i < layerCount)
{
if (tc != null)
{
tc.Text += "</tr><tr>";
}
}
}
}
实现上图所示的效果则数据源表头格式为:
报修区域,技术难度,当日|维修总费用|合计,当日|维修总费用|材料,当日|维修总费用|管理,当日|修次当日|耗时,当日|状态|合计,当日|状态|已完,当日|状态|未完,本月平均|维修总费用|合计,本月平均|维修总费用|材料,本月平均|维修总费用|管理,本月平均|修次,本月平均|耗时,本月平均|状态|合计,本月平均|状态|已完,本月平均|状态|未完
相关文章推荐
- ASP.NET(1):GridView动态绑定控件的处理方式。
- 如何让Gridview在没有数据的时候显示表头(asp.net)
- Asp.net 2.0 自定义控件开发[实现GridView多行表头固定表体滚动效果][示例代码下载]
- Asp.Net GridView 在编辑状态中使用动态Dropdownlist方法
- ASP.NET 2.0中合并GridView的表头单元格
- ASP.NET2005 设置GridView表头的背景图片
- Asp.Net下的DataGrid的多层表头(网友贴) 选择自 yoyo83 的 Blog
- [导入]ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- Asp.Net中页面运行时动态载入的UserControl内元素的事件处理的注意事项
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET-GridView之表头设计
- ASP.NET 2.0中GridView无限层复杂表头的实现
- 在asp.net中实现Gridview 表头固定
- ASP.NET 2.0 中动态添加 GridView 模板列 以及取值的例子
- Asp.net 2.0 自定义控件开发[实现GridView多行表头固定表体滚动效果][示例代码下载]
- ASP.NET 2.0中合并 GridView 的表头单元格
- ASP.NET 2.0 中动态添加 GridView 模板列的例子
- ASP.NET GridView 实现课程表显示(动态合并单元格)
- ASP.NET 2.0中GridView无限层复杂表头的实现