wlyyb521 发表于 2017-7-1 08:41:37

ASP.NET-GridView之表头设计

     我们 见过许多网页呈现表格的 时候,表头的形式多种多样。下面来看看,怎么制定多样的表头吧。
  效果显示:
  


      需要在后台写一个方法,网页一加载在前端写个事件调用这个方法。
  DEMO
  前端

<span style=&quot;font-family:Microsoft YaHei;font-size:18px;&quot;><asp:GridView ID=&quot;GridView1&quot; BorderColor=&quot;Black&quot; OnRowDataBound=&quot;GridView1_RowDataBound&quot;runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot;Font-Size=&quot;12px&quot; width=&quot;629px&quot; OnRowCreated=&quot;GridView1_RowCreated&quot;></span>

  后台

<span style=&quot;font-family:Microsoft YaHei;font-size:18px;&quot;>      /// <summary>
/// 在gridView控件中创建新行时发生,此事件通常用于创建某个行时修改改行的布局或外观
/// </summary>
/// <param name=&quot;sender&quot;></param>
/// <param name=&quot;e&quot;></param>
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.Attributes.Add(&quot;rowspan&quot;, &quot;2&quot;);
tcHeader.Attributes.Add(&quot;bgcolor&quot;, &quot;Azure&quot;);
tcHeader.Text = &quot;编号&quot;;
tcHeader.Add(new TableHeaderCell());
tcHeader.Attributes.Add(&quot;colspan&quot;, &quot;6&quot;);
tcHeader.Attributes.Add(&quot;bgcolor&quot;, &quot;Azure&quot;);
tcHeader.Text = &quot;基本信息&quot;;
tcHeader.Add(new TableHeaderCell());
tcHeader.Add(new TableHeaderCell());
tcHeader.Attributes.Add(&quot;bgcolor&quot;, &quot;Azure&quot;);
tcHeader.Text = &quot;福利</th></tr><tr>&quot;;
//第二行表头在第二行第三列开始,填满基本信息
tcHeader.Add(new TableHeaderCell());
tcHeader.Attributes.Add(&quot;bgcolor&quot;, &quot;Azure&quot;);
tcHeader.Text = &quot;账号&quot;;
tcHeader.Add(new TableHeaderCell());
tcHeader.Attributes.Add(&quot;bgcolor&quot;, &quot;Azure&quot;);
tcHeader.Text = &quot;姓名&quot;;
tcHeader.Add(new TableHeaderCell());
tcHeader.Attributes.Add(&quot;bgcolor&quot;, &quot;Azure&quot;);
tcHeader.Text = &quot;性别&quot;;
tcHeader.Add(new TableHeaderCell());
tcHeader.Attributes.Add(&quot;bgcolor&quot;, &quot;Azure&quot;);
tcHeader.Text = &quot;住址&quot;;
tcHeader.Add(new TableHeaderCell());
tcHeader.Attributes.Add(&quot;bgcolor&quot;, &quot;Azure&quot;);
tcHeader.Text = &quot;邮编&quot;;
tcHeader.Add(new TableHeaderCell());
tcHeader.Attributes.Add(&quot;bgcolor&quot;, &quot;Azure&quot;);
tcHeader.Text = &quot;生日&quot;;
tcHeader.Add(new TableHeaderCell());
tcHeader.Attributes.Add(&quot;bgcolor&quot;, &quot;Azure&quot;);
tcHeader.Text = &quot;月薪&quot;;
break;
}
}</span>



  感谢您的宝贵时间······
页: [1]
查看完整版本: ASP.NET-GridView之表头设计