建立跨多列、多行表頭的DataGrid

來源:互聯網
上載者:User

建立跨多列、多行表頭的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 &gt; 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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.