首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

datagrid 动态增添一行表头

2011-12-25 
datagrid 动态添加一行表头DataGridItem rowHeader new DataGridItem(0, -1, ListItemType.Header)rowH

datagrid 动态添加一行表头
DataGridItem rowHeader = new DataGridItem(0, -1, ListItemType.Header);
rowHeader.Height=30;
Literal newCells = new Literal();

newCells.Text =@"<colspan='3'>标题</th>";

TableCellCollection cells = e.Item.Cells;
TableHeaderCell headerCell = new TableHeaderCell();


headerCell.Controls.Add(newCells);
rowHeader.Cells.Add(headerCell);


rowHeader.Visible = true;

this.DataGrid1.Controls[0].Controls.AddAt(0, rowHeader);

这样加上的表头不是三个各合并在一起,而是以下形式:

 ---------------------------------------
| 标题 | |
 ---------------------------------------
| 语文 | 数学 | 化学 |
 ---------------------------------------



我想要这种样式:
 ---------------------------------------
| 标题 |
 ---------------------------------------
| 语文 | 数学 | 化学 |
 ---------------------------------------

代码上出什么差错了?望指点,谢谢!



[解决办法]
gridview中这样写可以
 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("colspan", "2"); //跨Column
tcHeader[0].Text = "标题</th></tr><tr>";

//第二行表头
tcHeader.Add(new TableHeaderCell());
tcHeader[1].Text = "语文";
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Text = "数学";
tcHeader.Add(new TableHeaderCell());
tcHeader[3].Text = "化学";
break;
}
}
datagrid应类似吧
[解决办法]
headerCell.ColumnSpan = 100;

[解决办法]

探讨
headerCell.ColumnSpan = 100;

[解决办法]
C# code
   public partial class WebForm7 : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {            DataTable dt = new DataTable();            dt.Columns.Add("Chinese", typeof(int));            dt.Columns.Add("Math",typeof(int));            dt.Columns.Add("Nature",typeof(int));            DataRow row = dt.NewRow();            row[0] = 100;            row[1] = 100;            row[2] = 99;            dt.Rows.Add(row);            this.GridView1.DataSource = dt;            this.GridView1.DataBind();        }        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)        {                        Table t = new Table();            if(e.Row.RowType == DataControlRowType.Header)            {                TableRow r = new TableRow();                TableCell c = new TableCell();                c.Text = "Title";                c.ColumnSpan = 3;                c.HorizontalAlign = HorizontalAlign.Center;                r.Cells.Add(c);                TableRow r2 = new TableRow();                TableCell c1 = new TableCell();                c1.Text = e.Row.Cells[0].Text;                r2.Cells.Add(c1);                TableCell c2 = new TableCell();                c2.Text = e.Row.Cells[1].Text;                r2.Cells.Add(c2);                TableCell c3 = new TableCell();                c3.Text = e.Row.Cells[2].Text;                r2.Cells.Add(c3);                t.Rows.Add(r);                t.Rows.Add(r2);                e.Row.Controls.Clear();                TableCell newc = new TableCell();                newc.ColumnSpan = 3;                newc.Controls.Add(t);                e.Row.Controls.Add(newc);            }        }    } 

热点排行