建立固定表頭、表格體滾動的DataGrid (轉)

gugu99發表於2007-12-11
建立固定表頭、表格體滾動的DataGrid (轉)[@more@]

建立固定表頭、表格體滾動的DataGrid

playground.com/">孟子E章

在使用DataGrid時,有時候需要表頭表頭固定、表格體的內容可以滾動,下面的程式碼就是實現這個功能的程式碼。

x" target=_blank>檢視例子

ShowFixedHeader.

.mengxianhui.com.ShowFixedHeader&quot;%&gt; w3c//DTD HTML 4.0 Transitional//EN&quot;&gt; <title id="lucky_elove" runat="server"></title> <meta name="GENERATOR" content="a href=" tag-126-1.html="">microsoft .NET 7.0&quot;&gt; <meta name="CODE_LANGUAGE" content="a href=" tag-50-1.html=""> 7.0&quot;&gt; <meta name="vs_defaultClientScript" content="a href=" tag-133-1.html="">script&quot;&gt; <meta name="vs_targetSchema" content=" href=" tag-340-1.html="">intellisense/&quot;&gt; <fo href="http://blog.itpub.net/10748419/viewspace-991314/tag-415-1.html">RM id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;&gt; Public Class ShowFixedHeader 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 Form1 As System.Web.UI.HtmlControls.HtmlForm Protected WithEvents lucky_elove As HtmlControls.HtmlGenericControl #Region &quot; Web Form Designer Generated Code &quot; 'This call is required by the Web Form Designer. <system href="http://blog.itpub.net/10748419/viewspace-991314/tag-268-1.html">buggerStepThrough()&gt; Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System., ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lucky_elove.InnerText = &quot;【孟憲會之精彩世界】 - 跨欄表頭的實現&quot; Table1.Rows(0).Cells(0).InnerText = &quot;【孟憲會之精彩世界】.NET版本之最新文章&quot; Table1.Rows(0).Cells(1).InnerText = &quot;文章資訊&quot; Table1.Rows(1).Cells(0).InnerText = &quot;文章標題&quot; Table1.Rows(1).Cells(1).InnerText = &quot;釋出時間&quot; Table1.Rows(1).Cells(2).InnerText = &quot;所屬欄目&quot; Table1.Rows(1).Cells(3).InnerText = &quot;點選率&quot; Table1.Rows(0).Style.Add(&quot;color&quot;, &quot;white&quot;) Table1.Rows(0).Style.Add(&quot;font-weight&quot;, &quot;bold&quot;) Table1.Rows(0).Cells(0).Attributes.Add(&quot;onclick&quot;,_ &quot;window.open('')&quot;) Table1.Rows(0).Cells(0).Style.Add(&quot;cursor&quot;, &quot;hand&quot;) Try Dim cnString As String = &quot;Provr=Microsoft.Jet.OLEDB.4.0;Data =&quot;_ + Server.MapPath(&quot;Test.mdb&quot;) Dim cn As New OleDbConnection(cnString) cn.Open() Dim str As String = &quot; TOP 30 D.Title,D.CreateDate,S.Title as pid,D.HitCount &quot;_ + &quot;FROM Document D INNER JOIN Subject S ON D.pid = S.id ORDER BY CreateDate DESC&quot; Dim cmd As New OleDbCommand(strSQL, cn) DataGrid1.DataSource = cmd.ExecuteReader DataGrid1.DataBind() cn.Close() cn = Nothing Catch eOle As OleDbException Response.Write(&quot;產生錯誤:&quot; + eOle.Message) End Try End Sub Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, _ ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then If e.Item.Cells(0).Text.Length &gt; 26 Then e.Item.Cells(0).Attributes.Add(&quot;Title&quot;, e.Item.Cells(0).Text) e.Item.Cells(0).Text = e.Item.Cells(0).Text.Substring(0, 26) + &quot;…&quot; End If e.Item.Cells(1).Text = Format(System.Convert.ToDateTime(e.Item.Cells(1).Text),_ &quot;yyyy年M月d日 h點m分s秒&quot;) End If End Sub End Class </system></fo>

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-991314/,如需轉載,請註明出處,否則將追究法律責任。

相關文章