日期:2012-05-07  浏览次数:20558 次

在使用DataGrid时,有时候需要表头跨列、多行,下面的代码就是实现这个功能的代码。

查看例子

ShowColSpanHeader.aspx

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ShowColSpanHeader.aspx.vb"
Inherits="aspxWeb.mengxianhui.com.ShowColSpanHeader"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title id="lucky_elove" runat=server></title>
<meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0">
<meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout" leftmargin="0">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" runat="server" cellSpacing="1" cellPadding="2" width="780" border="1"
bgcolor="#cc6633" bordercolor="#cc9966" style="FONT-SIZE:9pt;BORDER-BOTTOM:0px">
<TR align="center">
<TD colspan="2" width="580"></TD>
<TD width="200" colspan="2"></TD>
</TR>
<TR align="center">
<TD width="400" bgcolor="#66cc99"></TD>
<TD width="180" bgcolor="white"></TD>
<TD width="160" bgcolor="#99cccc"></TD>
<TD width="40" bgcolor="#009999"></TD>
</TR>
</TABLE>
<asp:DataGrid id="DataGrid1" width="780px" AlternatingItemStyle-BackColor="#6699ff"
CellPadding="2" CellSpacing="1" BorderWidth="1" BorderColor="#cc9966" Font-Size="9pt"
runat="server" ShowHeader="False" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="Title">
<ItemStyle Width="400px"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="CreateDate">
<ItemStyle Width="180px"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="pid">
<ItemStyle Width="160px"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="HitCount">
<ItemStyle Width="40px"></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>

ShowColSpanHeader.aspx.vb

Imports System
Imports System.Data
Imports System.Data.OleDb

Public Class ShowColSpanHeader
Inherits System.Web.UI.Page
Protected WithEvents Table1 As System.Web.UI.HtmlControls.HtmlTable
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents lucky_elove As HtmlControls.HtmlGenericControl
#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, By