[转]ASP.NET 2.0中GridView无限层复杂表头的实现

本文转自:http://blog.csdn.net/net_lover/article/details/1306211

实现方法就是给单元格填充我们想要的格式代码。

C#

<%@ Page Language="C#" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  // 计算数据,完全可以从数据看取得
  ICollection CreateDataSource( )
  {
    System.Data.DataTable dt = new System.Data.DataTable();
    System.Data.DataRow dr;
    dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
    dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
    dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
    dt.Columns.Add(new System.Data.DataColumn("数学", typeof(System.Decimal)));
    dt.Columns.Add(new System.Data.DataColumn("英语", typeof(System.Decimal)));
    dt.Columns.Add(new System.Data.DataColumn("计算机", typeof(System.Decimal)));

for (int i = 0; i < 8; i++)
    {
      System.Random rd = new System.Random(Environment.TickCount * i); ;
      dr = dt.NewRow();
      dr[0] = "班级" + i.ToString();
      dr[1] = "学生" + i.ToString();
      dr[2] = System.Math.Round(rd.NextDouble() * 100, 2);
      dr[3] = System.Math.Round(rd.NextDouble() * 100, 2);
      dr[4] = System.Math.Round(rd.NextDouble() * 100, 2);
      dr[5] = System.Math.Round(rd.NextDouble() * 100, 2);
      dt.Rows.Add(dr);
    }
    System.Data.DataView dv = new System.Data.DataView(dt);
    return dv;
  }

protected void Page_Load( object sender, EventArgs e )
  {
    if (!IsPostBack)
    {
      GridView1.BorderColor = System.Drawing.Color.DarkOrange;
      GridView1.DataSource = CreateDataSource();
      GridView1.DataBind();
    }
  }

protected void GridView1_RowCreated( object sender, GridViewRowEventArgs e )
  {

if (e.Row.RowType == DataControlRowType.Header)
    {
      //创建一个GridViewRow,相当于表格的 TR 一行
      GridViewRow rowHeader = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal);
      string HeaderBackColor = "#EDEDED";
      rowHeader.BackColor = System.Drawing.ColorTranslator.FromHtml(HeaderBackColor);

//实现确定要显示的表头样式,也可以通过计算生成

//    <tr>
      //      <td rowspan=‘2‘>关键单元格</td>
      //      <td colspan=‘2‘>表头文字</td>
      //      <td colspan=‘2‘>表头文字</td>
      //      <td>表头文字</td>
      //      </tr>
      //      <tr bgcolor=‘#FFF‘>
      //      <td colspan=‘2‘>表头文字</td>
      //      <td rowspan=‘2‘>表头文字</td>
      //      <td colspan=‘2‘>表头文字</td>
      //      </tr>
      //      <tr bgcolor=‘#FFF‘>
      //      <td>表头文字</td>
      //      <td>表头文字</td>
      //      <td>表头文字</td>
      //      <td>表头文字</td>
      //      <td>表头文字</td>";
      //   </tr>
      // 上面的样式可以设置斜线

Literal newCells = new Literal();
      newCells.Text = @"表头文字1</th>
                  <th colspan=‘2‘>表头文字2</th>
                  <th colspan=‘2‘>表头文字3</th>
                  <th>表头文字4</th>
                  </tr>
                  <tr bgcolor=‘" + HeaderBackColor + "‘>";
      newCells.Text += @"                         
                  <th colspan=‘2‘>表头文字5</th>
                  <th rowspan=‘2‘>表头文字6</th>
                  <th colspan=‘2‘>表头文字7</th>
                  </tr>
                  <tr bgcolor=‘" + HeaderBackColor + "‘>";
      newCells.Text += @"  
                  <th>表头文字8</th>
                  <th>表头文字9</th>
                  <th>表头文字10</th>
                  <th>表头文字11</th>
                  <th>表头文字12";

TableCellCollection cells = e.Row.Cells;
      TableHeaderCell headerCell = new TableHeaderCell();
      //下面的属性设置与 <td rowspan=‘2‘>关键单元格</td> 要一致
      headerCell.RowSpan = 2;
      headerCell.Controls.Add(newCells);
      rowHeader.Cells.Add(headerCell);

rowHeader.Cells.Add(headerCell);
      rowHeader.Visible = true;

//添加到 GridView1
      GridView1.Controls[0].Controls.AddAt(0, rowHeader);
    }
  }

protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e )
  {
    if (e.Row.RowType == DataControlRowType.Header)
    {
      e.Row.Attributes.Add("style", "background:#9999FF;color:#FFFFFF;font-size:14px");
    }
    else
    {
      e.Row.Attributes.Add("style", "background:#FFF");
    }
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>为 GridView 添加多层表头</title>
</head>
<body>
  <form id="Form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" CellSpacing="1" CellPadding="3" Font-Size="12px"
      Width="600px" BackColor="#000000" BorderWidth="0" OnRowDataBound="GridView1_RowDataBound"
      OnRowCreated="GridView1_RowCreated">
    </asp:GridView>
  </form>
</body>
</html>

VB.NET

<%@ Page Language="VB" AutoEventWireup="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  Function CreateDataSource() As ICollection
    Dim dt As System.Data.DataTable = New System.Data.DataTable
    Dim dr As System.Data.DataRow
    dt.Columns.Add(New System.Data.DataColumn("学生班级", GetType(System.String)))
    dt.Columns.Add(New System.Data.DataColumn("学生姓名", GetType(System.String)))
    dt.Columns.Add(New System.Data.DataColumn("语文", GetType(System.Decimal)))
    dt.Columns.Add(New System.Data.DataColumn("数学", GetType(System.Decimal)))
    dt.Columns.Add(New System.Data.DataColumn("英语", GetType(System.Decimal)))
    dt.Columns.Add(New System.Data.DataColumn("计算机", GetType(System.Decimal)))
    Dim i As Integer = 0
    While i < 8
      Dim rd As System.Random = New System.Random(Environment.TickCount * i)

dr = dt.NewRow
      dr(0) = "班级" + i.ToString
      dr(1) = "学生" + i.ToString
      dr(2) = System.Math.Round(rd.NextDouble * 100, 2)
      dr(3) = System.Math.Round(rd.NextDouble * 100, 2)
      dr(4) = System.Math.Round(rd.NextDouble * 100, 2)
      dr(5) = System.Math.Round(rd.NextDouble * 100, 2)
      dt.Rows.Add(dr)
      System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
    End While
    Dim dv As System.Data.DataView = New System.Data.DataView(dt)
    Return dv
  End Function

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not IsPostBack Then
      GridView1.BorderColor = System.Drawing.Color.DarkOrange
      GridView1.DataSource = CreateDataSource()
      GridView1.DataBind()
    End If
  End Sub

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.Header Then
      Dim rowHeader As GridViewRow = New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Normal)
      Dim HeaderBackColor As String = "#EDEDED"
      rowHeader.BackColor = System.Drawing.ColorTranslator.FromHtml(HeaderBackColor)
      Dim newCells As Literal = New Literal
      newCells.Text = "表头文字1</th>" + _
                      " <th colspan=‘2‘>表头文字2</th>" + _
                      " <th colspan=‘2‘>表头文字3</th>" + _
                      " <th>表头文字4</th>" + _
                      " </tr>" + _
                      " <tr bgcolor=‘" + HeaderBackColor + "‘>" + _
                      "  <th colspan=‘2‘>表头文字5</th>" + _
                      "  <th rowspan=‘2‘>表头文字6</th>" + _
                      " <th colspan=‘2‘>表头文字7</th>" + _
                      " </tr>" + _
                      " <tr bgcolor=‘" + HeaderBackColor + "‘>" + _
                      "  <th>表头文字8</th>" + _
                      "  <th>表头文字9</th>" + _
                      "  <th>表头文字10</th>" + _
                      "  <th>表头文字11</th>" + _
                      "  <th>表头文字12"
      Dim cells As TableCellCollection = e.Row.Cells
      Dim headerCell As TableHeaderCell = New TableHeaderCell
      headerCell.RowSpan = 2
      headerCell.Controls.Add(newCells)
      rowHeader.Cells.Add(headerCell)
      rowHeader.Cells.Add(headerCell)
      rowHeader.Visible = True
      GridView1.Controls(0).Controls.AddAt(0, rowHeader)
    End If
  End Sub

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If e.Row.RowType = DataControlRowType.Header Then
      e.Row.Attributes.Add("style", "background:#9999FF;color:#FFFFFF;font-size:14px")
    Else
      e.Row.Attributes.Add("style", "background:#FFF")
    End If
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>为 GridView 添加多层表头</title>
</head>
<body>
  <form id="Form1" runat="server">
    <asp:GridView ID="GridView1" runat="server" CellSpacing="1" CellPadding="3" Font-Size="12px"
      Width="600px" BackColor="#000000" BorderWidth="0" OnRowDataBound="GridView1_RowDataBound"
      OnRowCreated="GridView1_RowCreated">
    </asp:GridView>
  </form>
</body>
</html>

时间: 2024-07-29 02:26:53

[转]ASP.NET 2.0中GridView无限层复杂表头的实现的相关文章

asp.net 2.0中傻瓜式使用soap header

在websevrice 中,soap header是十分重要的哦,主要是安全性的考虑,在asp.net 2.0中,可以简单地应用soap header来进行傻瓜式的应用,更复杂的应用当然要更深入地去看了, 首先,我们写个简单的helloworld的webservice using System; using System.Web; using System.Web.Services; using System.Web.Services.Protocols; [WebService(Namespa

Asp.Net MVC3.0中防止跨站的POST

在Form中添加 @Html.AntiForgeryToken(); 在后台的Action中增加 [ValidateAntiForgeryToken] 这个方法还可以添加自定义的参数 @Html.AntiForgeryToken("SaltValue"); 后台的Action中,必需指名Token的值才允许正常提交. [ValidateAntiForgeryToken Salt=("SaltValue")] Asp.Net MVC3.0中防止跨站的POST,布布扣,

在asp.net 2.0中使用SqlBulkCopy类迁移数据

在asp.net 2.0中使用SqlBulkCopy类迁移数据 (转) http://jackyrong.cnblogs.com/archive/2005/08/29/225521.html 我们经常要在一个表中将数据迁移到另一个表,当然,用的方法十分多了.在.net 2.0中,提供了一个sqlbulkcopy类,也可以实现如下的操作,下面简单介绍下.比如一个表如下CREATE TABLE Person3( PersonID int IDENTITY(1,1) PRIMARY KEY, Name

使用asp.net 2.0中的SqlBulkCopy类批量复制数据

介绍:在软件开发中,把数据从一个地方复制到另一个地方是一个普遍的应用. 在很多不同的场合都会执行这个操作,包括旧系统到新系统的移植,从不同的数据库备份数据和收集数据. ASP.NET 2.0有一个SqlBulkCopy类,它可以帮助你从不同的数据源复制数据到SQL SERVER数据库. 本文中我将示范SqlBulkCopy类的不同应用. 数据库设计: 这个数据库的设计还是蛮简单的,它基于Northwind数据库的Products表.另外我还在Northwind数据库中创建了3个表. 详情可以看一

asp.net core1.x/asp.net core2.0中如何加载多个配置文件

写这篇文章,来简单的谈一下,asp.net core中,如何加载多配置文件,如有错误请斧正. 在1.x的时候,我们是自己配置 WebHostBuilder而在2.0的时候,ef core团队,将配置写到了一个CreateDefaultBuilder,这是一个预配置,人家为了方便做的. 原文地址:https://www.cnblogs.com/gdsblog/p/8503115.html

ASP.NET Core3.0 中的运行时编译

运行时编译 通过 Razor 文件的运行时编译补充生成时编译. 当 .cshtml 文件的内容发生更改时,ASP.NET Core MVC 将重新编译 Razor 文件 . 通过 Razor 文件的运行时编译补充生成时编译. RazorViewEngineOptions AllowRecompilingViewsOnFileChange 获取或设置一个值,该值确定当磁盘上的文件发生更改时是否重新编译和更新 Razor 文件(Razor 视图和 Razor Pages). 对于以下项,默认值为 t

在Asp.net 4.0 中动态注册HttpModule

using System; using System.Web; using Microsoft.Web.Infrastructure; namespace MvcApplication1 { public class CustomModule : IHttpModule { public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); } vo

Asp.net中GridView使用详解(引)

GridView无代码分页排序GridView选中,编辑,取消,删除GridView正反双向排序GridView和下拉菜单DropDownList结合GridView和CheckBox结合鼠标移到GridView某一行时改变该行的背景色方法一鼠标移到GridView某一行时改变该行的背景色方法二GridView实现删除时弹出确认对话框GridView实现自动编号GridView实现自定义时间货币等字符串格式GridView实现用“...”代替超长字符串GridView一般换行与强制换行GridV

ASP.Net4.0中新增23项功能

这篇文章介绍Visual Studio 2010 (ASP.Net 4.0)的新功能. 1.代码片段(Code Snippets): 代码段是预先开发的代码模板,可以节省我们对有关语法思考的时间.在VS 2005和VS 2008中,已经有建立了很多代码段.不过,这些只适用于隐藏代码(code behind).在VS 2010中代码片段支持JScript,HTML以及asp.net标记.在下面画面,展示了JScript和HTML片段的快捷菜单. 在JS中: 在Html中 : 2.New Profi