GridView动态创建列、多行表头
来源:互联网 发布:厂家寻找淘宝卖家合作 编辑:程序博客网 时间:2024/06/10 01:34
一、动态创建列:
//创建GridView列的方法
private void CreateGridColumn(string dataField, string headerText, int width, string headerStyle, string itemStyle)
{
BoundField bc = new BoundField();
bc.DataField = dataField;
bc.HeaderText = headerText;
bc.HeaderStyle.CssClass = headerStyle; //若有默认样式,此行代码及对应的参数可以移除
bc.ItemStyle.CssClass = itemStyle; //若有默认样式,此行代码及对应的参数可以移除
GridView1.Columns.Add(bc); //把动态创建的列,添加到GridView中
GridView1.Width = new Unit(GridView1.Width.Value + width); //每添加一列后,要增加GridView的总体宽度
}
{
BoundField bc = new BoundField();
bc.DataField = dataField;
bc.HeaderText = headerText;
bc.HeaderStyle.CssClass = headerStyle; //若有默认样式,此行代码及对应的参数可以移除
bc.ItemStyle.CssClass = itemStyle; //若有默认样式,此行代码及对应的参数可以移除
GridView1.Columns.Add(bc); //把动态创建的列,添加到GridView中
GridView1.Width = new Unit(GridView1.Width.Value + width); //每添加一列后,要增加GridView的总体宽度
}
在GridView绑定之前,调用以上方法:
//绑定GridView的数据
private void BindGrid()
{
#region 添加动态列
GridView1.Columns.Clear();
GridView1.Width = new Unit(0);
CreateGridColumn("name", "名称", 150, "cuntleft", "cuntleft");
private void BindGrid()
{
#region 添加动态列
GridView1.Columns.Clear();
GridView1.Width = new Unit(0);
CreateGridColumn("name", "名称", 150, "cuntleft", "cuntleft");
//再次调用CreateGridColumn方法,动态创建其他的列
//.......
#endregion
#endregion
//dt:数据源
GridView1.DataSource = dt;
GridView1.DataBind();
}
二、动态创建表头
在GridView的RowCreated事件中,添加以下代码:
//设计表头
protected void SmartGridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
TableCellCollection header = e.Row.Cells;
header.Clear();
string headtxt = "名称</th>";
headtxt += "<th colspan='4'>汇总</th>"; //跨四列
protected void SmartGridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
TableCellCollection header = e.Row.Cells;
header.Clear();
string headtxt = "名称</th>";
headtxt += "<th colspan='4'>汇总</th>"; //跨四列
headtxt += "<th>星期一</th><th>星期二</th><th>星期三</th><th>星期四</th>";
headtxt = headtxt.Substring(0, headtxt.Length - 5); //移除掉最后一个</th>
TableHeaderCell cell = new TableHeaderCell();
cell.Attributes.Add("rowspan", "2"); //跨两行
cell.Text = (headtxt);
header.Add(cell);
headtxt = headtxt.Substring(0, headtxt.Length - 5); //移除掉最后一个</th>
TableHeaderCell cell = new TableHeaderCell();
cell.Attributes.Add("rowspan", "2"); //跨两行
cell.Text = (headtxt);
header.Add(cell);
}
}
}
最后的显示的GridView表头结果如下:
- GridView动态创建列、多行表头
- GridView动态创建列、多行表头
- 动态创建DataTable,GridView创建多表头,表头跨行或跨列合并,创建计算列及列内容自适应等
- 动态创建Gridview多表头和尾末统计行
- GridView多行表头创建
- 动态创建GridView绑定列
- 动态创建GridView绑定列
- 动态创建GridView模版列
- gridview多行表头
- GridView多行表头
- GridView多行表头
- ExtJs 实现动态列,动态多表头
- ExtJs 实现动态列,动态多表头
- VS2005 GridView动态绑定数据源(交叉表) 动态绑定列 动态生成模板列 自定义表头
- VS2005 GridView动态绑定数据源(交叉表) 动态绑定列 动态生成模板列 自定义表头
- Gridview创建表头
- GridView 创建自定义表头
- GridView动态创建模板列templateField
- PNG透明背景按钮的实现(MFC)
- hdu 2845 Beans(dp)
- MyEclipse下XFire开发Webservice实例
- Bluez交叉编译移植到arm linux
- 【2007年提高组3】矩阵取数游戏
- GridView动态创建列、多行表头
- Asp.net中Word文件转换成HTML
- 远程桌面按键后出现Windows 键的功能?教你方法解决!
- 经典人事面试70问
- MIPS MMU和 Memory Management
- How to do android emma coverage test in your own ant scipts
- PHP环境方面
- ajax 与 jquery ajax
- 创建文本文档