設定 GridView 內框線的顏色

設定 GridView 內框線的顏色

GridView 控制項沒有提供屬性設定內框線的顏色,不過我們可以設定 GridView 的 Attributes("bordercolor") 來達到這個需求。

以下的範例就是將 GridView 的內框線設為藍色。

   1:      Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
   2:          GridView1.Attributes("bordercolor") = System.Drawing.ColorTranslator.ToHtml(Drawing.Color.Blue)
   3:      End Sub
 
 
我們也可以擴展 GridView 控制項,加入一個 GridLineColor 屬性,用來設定 GridView 的內框線顏色。
 
   1:  Imports System
   2:  Imports System.Collections.Generic
   3:  Imports System.ComponentModel
   4:  Imports System.Text
   5:  Imports System.Web
   6:  Imports System.Web.UI
   7:  Imports System.Web.UI.WebControls
   8:  Imports System.Drawing
   9:   
  10:  < _
  11:  Description("GridView 控制項"), _
  12:  ToolboxData("<{0}:TBGridView runat=server></{0}:TBGridView>") _
  13:  > _
  14:  Public Class TBGridView
  15:      Inherits GridView
  16:      Private FGridLineColor As Color
  17:   
  18:      ''' <summary>
  19:      ''' GridView 格線顏色。
  20:      ''' </summary>
  21:      < _
  22:      Description("GridView 格線顏色"), _
  23:      TypeConverter(GetType(WebColorConverter)), _
  24:      Category("Appearance"), _
  25:      DefaultValue(GetType(Color), "")> _
  26:      Public Property GridLineColor() As Color
  27:          Get
  28:              Return FGridLineColor
  29:          End Get
  30:          Set(ByVal value As Color)
  31:              FGridLineColor = value
  32:          End Set
  33:      End Property
  34:   
  35:      ''' <summary>
  36:      ''' 輸出用戶端檢視的 HTML 碼。
  37:      ''' </summary>
  38:      ''' <param name="writer">用來在用戶端瀏覽器上呈現伺服器控制項內容。</param>
  39:      Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
  40:          If Me.GridLineColor <> Drawing.Color.Empty Then
  41:              Me.Attributes("bordercolor") = System.Drawing.ColorTranslator.ToHtml(Me.GridLineColor)
  42:          End If
  43:          MyBase.Render(writer)
  44:      End Sub
  45:  End Class

 

 

 

 

ASP.NET 魔法學院