VB.NET中關於DataGrid顏色的自定義。 (轉)

amyz發表於2007-08-15
VB.NET中關於DataGrid顏色的自定義。 (轉)[@more@]

 

  近來專案用到了一個類似DataGrid中自定義行或列的顏色的功能,然而應用卻是在WIN的窗體下,實現起來無法使用類似script的指令碼註冊的功能來動態完成,十分著急,察看了CSDN的一些關於下的關於DataGrid的資料,看到這樣的一篇介紹DG結構的美文,題目是《Henry手記:WinFoDatagrid結構剖析》,作者是韓睿(Latitude),其中介紹了WIN  DG的顏色的定義,但是主要是針對每一個Cell的。

我們需要的則是標記某一行的資料,用顏色突出顯示,所以作了部分改動,現在把部分程式碼張貼出來供大家參考:

1.  基礎類出自韓睿:

URL: http://www.csdn/develop/read_article.?id=15686

  Public Class DataGridColoredTextBoxColumn:namespace prefix = o ns = "urn:schemas--com::office" />

 

  Inherits DataGridTextBoxColumn

 

  Public rowcollection As New Collection()

 

  Public BackColor() As Color

 

  Public ForeColor() As Color

 

 

  Private Function GetText(ByVal Value As ) As String

  If TypeOf (Value) Is System.Null Then

  Return NullText

  ElseIf Value Is Nothing Then

  Return ""

  Else

  Return Value.ToString

  End If

  End Function

 

  Protected Overloads Overrs Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, _

  ByVal As System..Forms.CurrencyManager, _

  ByVal rowNum As Integer, _

  ByVal backBrush As System.Drawing.Brush, _

   ByVal foreBrush As System.Drawing.Brush, _

  ByVal alignToRight As Boolean)

 

 

  Dim text As String

 

  text = GetText(GetColumnValueAtRow(source, rowNum))

 

  backBrush = New SolidBrush(TextBox.BackColor)

 

  foreBrush = New SolidBrush(TextBox.ForeColor)

 

 

  ReDim Preserve BackColor(rowcollection.Count)

 

  ReDim Preserve ForeColor(rowcollection.Count)

 

 

 

  Dim i As Integer = 1

 

  Do While (i <= rowcollection.Count)

 

  If rowNum = Val(rowcollection.Item(i)) Then

 

  If Not BackColor(i - 1).IsEmpty Then

 

  backBrush = New SolidBrush(BackColor(i - 1))

 

   End If

 

  If Not ForeColor(i - 1).IsEmpty Then

 

  foreBrush = New SolidBrush(ForeColor(i - 1))

 

  End If

 

  End If

 

  i += 1

 

  L

 

  MyBase.PaintText(g, bounds, text, backBrush, foreBrush, alignToRight)

 

  End Sub

 

  End Class

 

 

2.關於行顏色定義的類:

Imports System.Windows.Forms

Namespace Truck_WEB

 

  Public Class DrawDGClass

 

  Public Class ReDrawDataDridControls : Inherits DataGridColoredTextBoxColumn

 

  Public Sub DrawCorol(ByRef DG As DataGrid, Optional ByVal CurrentRowindex As Integer = 0)

  '設定選中的行的顏色,預設是第一行選中。

  Dim dt As DataTable

  Dim ts As New DataGridTableStyle()

  ts.AllowSorting = False

  Dim aColumnTextColumn As DataGridColoredTextBoxColumn

  dt = CType(DG.DataSource, DataTable)

  ts.Mappiame = CType(DG.DataSource, DataTable).TableName

  DG.TableStyles.Clear()

  Dim numCols As Integer

  numCols = dt.Columns.Count

   Dim i, j As Integer

  i = 0

  j = 0

 

  Do While (i < numCols)

 

  aColumnTextColumn = New DataGridColoredTextBoxColumn()

 

  Dim rowindex As Integer = 0

 

 

   For rowindex = 0 To dt.Rows.Count - 1

  Dim StrSel As String

  Dim MyForeCorol, MyBackCorol As Color

  aColumnTextColumn.rowcollection.Add(rowindex)

  If rowindex = CurrentRowindex Then

  MyForeCorol = Color.White

  MyBackCorol = Color.DarkSlateBlue

  else

 

  MyForeCorol = Color.DarkSlateBlue

   MyBackCorol = Color.White

  End If

 

  ReDim Preserve aColumnTextColumn.ForeColor(aColumnTextColumn.rowcollection.Count)

  ReDim Preserve aColumnTextColumn.BackColor(aColumnTextColumn.rowcollection.Count)

  aColumnTextColumn.ForeColor(rowindex) = MyForeCorol

  aColumnTextColumn.BackColor(rowindex) = MyBackCorol

  Next

 

 

  '要更改列頭名,請改下句的HeaderText值

  aColumnTextColumn.HeaderText = dt.Columns(i).ColumnName

 

  aColumnTextColumn.MapName = dt.Columns(i).ColumnName

 

  ts.GridColumnStyles.Add(aColumnTextColumn)

   i = (i + 1)

  Loop

  DG.TableStyles.Add(ts)

 

  End Sub

  End Class

  End Class

End Namespace

 

 

以上是設定選中單行的顏色為反色,各位還可以借題發揮一下!例如設定顏色,等等。

在此向《Henry手記:WinForm Datagrid結構剖析》的作者韓睿致謝!

 

以後我會盡量完善這個DrawDG的類,為大家提供方便!


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

相關文章