GridView如何动态生成表头?
要实现的表头如下:编号,姓名,课程1,课程2,课程3,课程4,课程5,课程6..
注释:
编号和姓名是从B表读出来的..
课程1-课程6 是用A表(课程表)里面读出来的..
如何实现这样的效果?.. 请高手们指教.... --------------------编程问答-------------------- http://topic.csdn.net/u/20091122/14/66db875a-8300-4c23-b94d-2e493b9d9c9f.html --------------------编程问答-------------------- 用sql的别名可以搞定 --------------------编程问答-------------------- 给你个大概思路,至于表头名称从数据库动态读取,你再自己写
1、设置gridView ShowHeader=false;
2、编写gridview 的RowCreated事件:
protected void gridView_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
#region 绑定表头
TableCell cell = new TableCell();
//表头行
GridViewRow head = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
cell = new TableCell();
cell.Text = "编号";
cell.RowSpan = 1;
cell.HorizontalAlign = HorizontalAlign.Center;
head.Cells.Add(cell);
cell = new TableCell();
cell.Text = "姓名";
cell.RowSpan = 1;
cell.HorizontalAlign = HorizontalAlign.Center;
head.Cells.Add(cell);
cell = new TableCell();
cell.Text = "课程一";
cell.RowSpan = 1;
cell.HorizontalAlign = HorizontalAlign.Center;
head.Cells.Add(cell);
……//略
……
this.gridView.Controls[0].Controls.AddAt(0, head);
#endregion
}
}
--------------------编程问答-------------------- 恩,这个方法应该可以基本上实现lz的要求。。。 --------------------编程问答-------------------- protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
//判断是否表头
case DataControlRowType.Header:
//第一行表头
TableCellCollection tcHeader = e.Row.Cells;
tcHeader.Clear();
tcHeader.Add(new TableHeaderCell());
tcHeader[0].Attributes.Add("rowspan", "2");
tcHeader[0].Attributes.Add("bgcolor", "Azure");
tcHeader[0].Text = "用户ID";
tcHeader.Add(new TableHeaderCell());
tcHeader[1].Attributes.Add("colspan", "4");
tcHeader[1].Attributes.Add("bgcolor", "Azure");
tcHeader[1].Text = "基 本 信 息</th></tr><tr>";
//第二行表头
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Attributes.Add("bgcolor", "Azure");
tcHeader[2].Text = "用户姓名";
tcHeader.Add(new TableHeaderCell());
tcHeader[3].Attributes.Add("bgcolor", "Azure");
tcHeader[3].Text = "性别";
tcHeader.Add(new TableHeaderCell());
tcHeader[4].Attributes.Add("bgcolor", "Azure");
tcHeader[4].Text = "家庭住址";
tcHeader.Add(new TableHeaderCell());
tcHeader[5].Attributes.Add("bgcolor", "Azure");
tcHeader[5].Text = "邮政编码";
break;
}
} --------------------编程问答-------------------- --------------------编程问答-------------------- http://www.cnblogs.com/cwr0723/archive/2008/07/18/1245593.htmlhttp://www.cnblogs.com/cwr0723/archive/2008/07/18/1245593.html --------------------编程问答-------------------- 也可以直接绑定表头文本
<%=(全局变量)%> --------------------编程问答-------------------- 测试了下.. 表头没显示出来呀...
--------------------编程问答-------------------- 我用的是access数据库... --------------------编程问答-------------------- 最简单的方法,你不同的sql语句获取字段直接用as给别名就行了。。。
select firstname as 姓 from name
.... --------------------编程问答-------------------- 课程表的数据如下:
课程编号 课程名称
1 课程1
2 课程2
3 课程3
... ...
B表的数据如下:
学员编号 学员姓名 课程名称
001 aaa 课程1
002 bbb 课程1
001 aaa 课程2
001 aaa 课程3
001 aaa 课程4
002 bbb 课程2
002 bbb 课程3
002 bbb 课程4
003 ccc 课程1
003 ccc 课程2
003 ccc 课程3
我要显示的效果是:
编号 姓名 课程1 课程2 课程3 课程4 课程5 课程6
--------------------编程问答-------------------- 不是这样滴哦...
你看我上面 B表和课程表的内容就知道啦...
--------------------编程问答-------------------- if (!IsPostBack)
{
emptyProcess();
}
private void emptyProcess()
{
DataTable dt = new DataTable();
DataTable tbl = BLL.LearnPlan.GetSCMInfo(); //获取课程名称
if (tbl != null)
{
DataColumn col = null;
col = new DataColumn("学员编号");
dt.Columns.Add(col);
col = new DataColumn("学员姓名");
dt.Columns.Add(col);
foreach (DataRow row in tbl.Rows)
{
col = new DataColumn(row["Course_name"].ToString());
dt.Columns.Add(col);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
我这样写... 表头不显示出来.... --------------------编程问答-------------------- 你要实现的效果可以在查询数据库的时候用stuff函数实现,将课程放到一列
然后页面代码处理split函数将其分开就行
具体的在网上查一下哈……
(我也是找资料看到你的,就给你一点思路)
补充:.NET技术 , ASP.NET