建立跨多列、多行表頭的DataGrid
孟子E章
在使用DataGrid時,有時候需要表頭跨列、多行,下面的代碼就是實現這個功能的代碼。
查看例子
ShowColSpanHeader.aspx
<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">
ShowColSpanHeader.aspx.vb
Imports System<br />Imports System.Data<br />Imports System.Data.OleDb<br />Public Class ShowColSpanHeader<br /> Inherits System.Web.UI.Page<br /> Protected WithEvents Table1 As System.Web.UI.HtmlControls.HtmlTable<br /> Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid<br /> Protected WithEvents lucky_elove As HtmlControls.HtmlGenericControl<br />#Region " Web Form Designer Generated Code "<br /> 'This call is required by the Web Form Designer.<br /> <system.diagnostics.debuggerstepthrough> Private Sub InitializeComponent()<br /> End Sub<br /> Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) _<br /> Handles MyBase.Init<br /> 'CODEGEN: This method call is required by the Web Form Designer<br /> 'Do not modify it using the code editor.<br /> InitializeComponent()<br /> End Sub<br />#End Region<br /> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _<br /> Handles MyBase.Load<br /> lucky_elove.InnerText = "【孟憲會之精彩世界】 - 跨欄表頭的實現"<br /> Table1.Rows(0).Cells(0).InnerText = "【孟憲會之精彩世界】.NET版本之最新文章"<br /> Table1.Rows(0).Cells(1).InnerText = "文章資訊"<br /> Table1.Rows(1).Cells(0).InnerText = "文章標題"<br /> Table1.Rows(1).Cells(1).InnerText = "發布時間"<br /> Table1.Rows(1).Cells(2).InnerText = "所屬欄目"<br /> Table1.Rows(1).Cells(3).InnerText = "點擊率"<br /> Table1.Rows(0).Style.Add("color", "white")<br /> Table1.Rows(0).Style.Add("font-weight", "bold")<br /> Table1.Rows(0).Cells(0).Attributes.Add("onclick", _<br /> "window.open('http://lucky_elove.www1.dotnetplayground.com/')")<br /> Table1.Rows(0).Cells(0).Style.Add("cursor", "hand")<br /> Try<br /> Dim cnString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" _<br /> + Server.MapPath("Test.mdb")<br /> Dim cn As New OleDbConnection(cnString)<br /> cn.Open()<br /> Dim strSQL As String = "SELECT TOP 20 D.Title,D.CreateDate,S.Title as pid,D.HitCount "_<br /> + "FROM Document D INNER JOIN Subject S ON D.pid = S.id ORDER BY CreateDate DESC"<br /> Dim cmd As New OleDbCommand(strSQL, cn)<br /> DataGrid1.DataSource = cmd.ExecuteReader<br /> DataGrid1.DataBind()<br /> cn.Close()<br /> cn = Nothing<br /> Catch eOle As OleDbException<br /> Response.Write("產生錯誤:" + eOle.Message)<br /> End Try<br /> End Sub<br /> Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _<br /> ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound<br /> If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then<br /> If e.Item.Cells(0).Text.Length > 26 Then<br /> e.Item.Cells(0).Attributes.Add("Title", e.Item.Cells(0).Text)<br /> e.Item.Cells(0).Text = e.Item.Cells(0).Text.Substring(0, 26) + "…"<br /> End If<br /> e.Item.Cells(1).Text=Format(System.Convert.ToDateTime(e.Item.Cells(1).Text),"yyyy年M月d日 h點m分s秒")<br /> End If<br /> End Sub<br />End Class<br /></system.diagnostics.debuggerstepthrough>