当前位置: 代码迷 >> ASP.NET >> asp.net动态生成GridView表头,该怎么解决
  详细解决方案

asp.net动态生成GridView表头,该怎么解决

热度:1548   发布时间:2013-02-25 00:00:00.0
asp.net动态生成GridView表头
在gridView中表头是从数据库中查出来的在表头下方的数据也是从数据库中查出来的,要一一对应。哪位知道应该怎么做啊? 

  原材料耗用量 
  导体 导体屏蔽 绝缘 填充  
型号 规格 数量 单价 金额 数量 单价 金额 数量 单价 金额 数量 单价 金额 材料成本
SDF 120*13 12 10 120 10 22 220 100 55 5500 100 8 800 6640

------解决方案--------------------------------------------------------
GridView合并表头多重表头无错完美版(以合并3列3行举例)
------解决方案--------------------------------------------------------
gridview多表头
protected void GridView1_OnRowCreated(object sender, GridViewRowEventArgs e)//表头数据 

if (e.Row.RowType == DataControlRowType.Header) 

TableCellCollection tcHeader = e.Row.Cells; 
tcHeader.Clear(); 

tcHeader.Add(new TableHeaderCell()); 
tcHeader[0].Attributes.Add("rowspan", "2"); 
tcHeader[0].Text = "编号"; 

}
http://topic.csdn.net/u/20090402/13/98a5b344-e53b-4aaa-89ae-f719ea45be4d
------解决方案--------------------------------------------------------
C# code
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)  ...{    if (e.Row.RowType == DataControlRowType.Header)    ...{      GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);      rowHeader.BackColor = System.Drawing.Color.White;      rowHeader.Font.Bold = true;      TableCellCollection cells = e.Row.Cells;      TableCell headerCell = new TableCell();      headerCell.Text = "";      rowHeader.Cells.Add(headerCell);      headerCell = new TableCell();      headerCell.Text = "学生成绩";      headerCell.ColumnSpan = cells.Count - 1;      headerCell.HorizontalAlign = HorizontalAlign.Center;      rowHeader.Cells.Add(headerCell);      rowHeader.Visible = true;      GridView1.Controls[0].Controls.AddAt(0, rowHeader);    }  }  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  ...{    e.Row.Attributes.Add("style", "background:#FFF");  }
------解决方案--------------------------------------------------------
说实话,我本人特别讨厌这个实现。不过还是贴出来吧。
效果


C# code
protected void Page_Load(object sender, EventArgs e){    if (!IsPostBack)    {            GridView1.DataSource = GetTable();            GridView1.DataBind();    }}private DataTable GetTable(){        DataTable dt = new DataTable();        DataColumn dc = new DataColumn();        dc.ColumnName = "ID";        dc.AutoIncrement = true;        dc.AutoIncrementSeed = 1;        dc.AutoIncrementStep = 1;        dt.Columns.Add(dc);        dt.PrimaryKey = new DataColumn[] { dc };        dt.Columns.Add("FirstName", typeof(string));        dt.Columns.Add("LastName", typeof(string));        dt.Columns.Add("City", typeof(string));        dt.Columns.Add("Street", typeof(string));        DataRow dr = dt.NewRow();        dr["FirstName"] = "元甲";        dr["LastName"] = "霍";        dr["City"] = "上海";        dr["Street"] = "南京路325号";        dt.Rows.Add(dr);        dr = dt.NewRow();        dr["FirstName"] = "真";        dr["LastName"] = "陈";        dr["City"] = "佛山";        dr["Street"] = "东山路15号";        dt.Rows.Add(dr);        return dt;}protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e){        if (e.Row.RowType == DataControlRowType.Header)        {            // 第一行表头            TableCellCollection tcHeader = e.Row.Cells;            tcHeader.Clear();            tcHeader.Add(new TableHeaderCell());            tcHeader[0].Attributes.Add("rowspan", "3"); //跨三行。            tcHeader[0].Text = "人员ID";            tcHeader.Add(new TableHeaderCell());            tcHeader[1].Attributes.Add("colspan", "4"); //跨四列            tcHeader[1].Text = "基本资料</th></tr><tr>"; // 作弊            // 第二行表头            tcHeader.Add(new TableHeaderCell());            tcHeader[2].Attributes.Add("colspan", "2");            tcHeader[2].Text = "姓名";                        tcHeader.Add(new TableHeaderCell());            tcHeader[3].Attributes.Add("colspan", "2");                        tcHeader[3].Text = "联系方式</th></tr><tr>"; //作弊            // 第三行表头            tcHeader.Add(new TableHeaderCell());            tcHeader[4].Text = "名";            tcHeader.Add(new TableHeaderCell());            tcHeader[5].Text = "姓";            tcHeader.Add(new TableHeaderCell());            tcHeader[6].Text = "城市";            tcHeader.Add(new TableHeaderCell());            tcHeader[7].Text = "街道";                                }}