日期:2014-05-17 浏览次数:20454 次
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"); }
------解决方案--------------------
说实话,我本人特别讨厌这个实现。不过还是贴出来吧。
效果
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) { // 第一行表头 TableCellCo