关于Gridview 表头自定义格式显示的问题
各位大虾,我想在程序里动态的定义gridview的表头单元格的样式,把表头(headerRow)的单元格按(dataRow)中的字段项进行合并或拆分,gridview中的列也是动态的显示即可以自定义显示那些列。类似图:
我想动态的控件每一列,同时可增加其它列。该怎么写呢,头都大了,搞了好几天都没有搞定,急啊!
--------------------编程问答-------------------- 这样的话,还是写代码构造一个Table的HTML吧。 --------------------编程问答--------------------
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", "3"); //跨Row
tcHeader[0].Attributes.Add("bgcolor", "white");
tcHeader[0].Text = "";
tcHeader.Add(new TableHeaderCell());
//tcHeader[1].Attributes.Add("bgcolor", "Red");
tcHeader[1].Attributes.Add("colspan", "6"); //跨Column
tcHeader[1].Text = "全部信息</th></tr><tr>";
//第二行表头
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Attributes.Add("bgcolor", "DarkSeaGreen");
tcHeader[2].Text = "身份证号码";
tcHeader.Add(new TableHeaderCell());
tcHeader[3].Attributes.Add("bgcolor", "LightSteelBlue");
tcHeader[3].Attributes.Add("colspan", "2");
tcHeader[3].Text = "基本信息";
tcHeader.Add(new TableHeaderCell());
tcHeader[4].Attributes.Add("bgcolor", "DarkSeaGreen");
tcHeader[4].Text = "福利";
tcHeader.Add(new TableHeaderCell());
tcHeader[5].Attributes.Add("bgcolor", "LightSteelBlue");
tcHeader[5].Attributes.Add("colspan", "2");
tcHeader[5].Text = "联系方式</th></tr><tr>";
//第三行表头
tcHeader.Add(new TableHeaderCell());
tcHeader[6].Attributes.Add("bgcolor", "Khaki");
tcHeader[6].Text = "身份证号码";
tcHeader.Add(new TableHeaderCell());
tcHeader[7].Attributes.Add("bgcolor", "Khaki");
tcHeader[7].Text = "姓名";
tcHeader.Add(new TableHeaderCell());
tcHeader[8].Attributes.Add("bgcolor", "Khaki");
tcHeader[8].Text = "出生日期";
tcHeader.Add(new TableHeaderCell());
tcHeader[9].Attributes.Add("bgcolor", "Khaki");
tcHeader[9].Text = "薪水";
tcHeader.Add(new TableHeaderCell());
tcHeader[10].Attributes.Add("bgcolor", "Khaki");
tcHeader[10].Text = "家庭住址";
tcHeader.Add(new TableHeaderCell());
tcHeader[11].Attributes.Add("bgcolor", "Khaki");
tcHeader[11].Text = "邮政编码";
break;
}
}
}
参考:http://blog.csdn.net/21aspnet/archive/2007/03/25/1540301.aspx 第15段功能描述 --------------------编程问答-------------------- to:tx5188,谢谢你提供的代码,上面表头我能写出来,但现在的问题是如果gridview中的列是动态显示的,即列的顺序不定 --------------------编程问答-------------------- 这个问题我也在想哦 --------------------编程问答-------------------- 谁 去 解决 一下这个问题 : http://topic.csdn.net/u/20090304/17/8383fe32-c9c1-443d-874d-a5f87c3a9703.html?seed=2101368538 --------------------编程问答-------------------- http://topic.csdn.net/u/20090304/17/8383fe32-c9c1-443d-874d-a5f87c3a9703.html?seed=2101368538
补充:.NET技术 , ASP.NET