protected void GridView1_RowCreated( object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.Header) { //因为自定义表头,所以原来系统中实现的排序功能就失效了需自行实现排序功能 TableCellCollection myTcl = e.Row.Cells; //清除自动生成的表头 myTcl.Clear(); //添加新的表头 Label l1 = new Label(); l1.Text = "序号"; myTcl[0].Controls.Add(l1); myTcl.Add(new TableHeaderCell()); myTcl[1].RowSpan = 2; myTcl[1].Text = "进口料件</br>品名"; myTcl[1].Width = 150; myTcl.Add(new TableHeaderCell()); myTcl[2].ColumnSpan = 2; myTcl[2].Text = "成品1"; myTcl.Add(new TableHeaderCell()); myTcl[3].ColumnSpan = 2; myTcl[3].Text = "成品2"; myTcl.Add(new TableHeaderCell()); myTcl[4].ColumnSpan = 2; myTcl[4].Text = "成品3"; myTcl.Add(new TableHeaderCell()); myTcl[5].ColumnSpan = 2; myTcl[5].Text = "成品4"; myTcl.Add(new TableHeaderCell()); myTcl[6].ColumnSpan = 2; myTcl[6].Text = "成品5"; myTcl.Add(new TableHeaderCell()); myTcl[7].ColumnSpan = 2; myTcl[7].Text = "成品6"; myTcl.Add(new TableHeaderCell()); myTcl[8].ColumnSpan = 2; myTcl[8].Text = "成品7"; myTcl.Add(new TableHeaderCell()); myTcl[9].ColumnSpan = 2; myTcl[9].Text = "成品8"; myTcl.Add(new TableHeaderCell()); myTcl[10].RowSpan = 2; myTcl[10].Text = "操作"; //以下为新增第二行标题,原理有些类似于SQL注入攻击 myTcl.Add(new TableHeaderCell()); myTcl[11].ColumnSpan = 1; myTcl[11].Text = "<tr><th>单耗</th><th>损耗</th><th>单耗</th><th>损耗</th><th>单耗</th><th>损耗</th><th>单耗</th><th>损耗</th><th>单耗</th><th>损耗</th><th>单耗</th><th>损耗</th><th>单耗</th><th>损耗</th><th>单耗</th><th>损耗</th>"; } } 转载于:https://www.cnblogs.com/Fortunelee/archive/2006/12/01/579097.html