GridView如何动态生成表头? 要实现的表头如下:编号,姓名,课程1,课程2,课程3,课程4,课程5,课程6..注释:编号和姓名是从B表读出来的..课程1-课程6 是用A表(课程表)里面读出来的..如何实现这样的效果?.. 请高手们指教.... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://topic.csdn.net/u/20091122/14/66db875a-8300-4c23-b94d-2e493b9d9c9f.html 给你个大概思路,至于表头名称从数据库动态读取,你再自己写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 } } 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 课程12 课程23 课程3... ...B表的数据如下:学员编号 学员姓名 课程名称001 aaa 课程1002 bbb 课程1001 aaa 课程2001 aaa 课程3001 aaa 课程4002 bbb 课程2002 bbb 课程3002 bbb 课程4003 ccc 课程1003 ccc 课程2003 ccc 课程3我要显示的效果是:编号 姓名 课程1 课程2 课程3 课程4 课程5 课程6 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函数将其分开就行具体的在网上查一下哈……(我也是找资料看到你的,就给你一点思路) asp.net word导入导出功能 构造的SQL语句里面包含‘字符,如何处理呢 如何修改另一个文件中的文本 关于 PetShop 4.0 的设计思想 存储过程的其中一个参数是一张表,在.net里面参数要用什么类型 光标定位只固定在一个控件上! 在上海工作4年了,想进外企,考个MCAD有没有用? 如何从数据库中循环读出某个字段,然后再textbox中显示出来? 现在asp.net webform 在公司里项目都是三层架构? 关于web语言的问题 怎么查询空字段!!!! 请教水晶报表错位问题,急!!!
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
}
}
{
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;
}
}
<%=(全局变量)%>
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
{
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();
}
}
我这样写... 表头不显示出来....
然后页面代码处理split函数将其分开就行
具体的在网上查一下哈……
(我也是找资料看到你的,就给你一点思路)