熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

VB.NET中關於DataGrid顏色的自定義

2013-11-13 10:01:46  來源: .NET編程 

  近來項目用到了一個類似WEB控件DataGrid中自定義行或列的顏色的功能然而應用卻是在WIN的窗體下實現起來無法使用類似JavaScript的腳本注冊的功能來動態完成十分著急察看了CSDN的一些關於WinForm下的關於DataGrid的資料看到這樣的一篇介紹DG結構的美文題目是《Henry手記:WinForm Datagrid結構剖析》作者是韓睿(Latitude)其中介紹了WIN DG的顏色的定義但是主要是針對每一個Cell的
  
  我們需要的則是標記某一行的數據用顏色突出顯示所以作了部分改動現在把部分代碼張貼出來供大家參考
  
  . 基礎類出自韓睿
  
  URL: ?id=
  
   Public Class DataGridColoredTextBoxColumn 

   Inherits DataGridTextBoxColumn 

   Public rowcollection As New Collection() 

   Public BackColor() As Color 

   Public ForeColor() As Color 

   Private Function GetText(ByVal Value As Object) As String
  
   If TypeOf (Value) Is SystemDBNull Then
  
   Return NullText
  
   ElseIf Value Is Nothing Then
  
   Return
  
   Else
  
   Return ValueToString
  
   End If
  
   End Function 

   Protected Overloads Overrides Sub Paint(ByVal g As SystemDrawingGraphics ByVal bounds As SystemDrawingRectangle _
  
   ByVal source As SystemWindowsFormsCurrencyManager _
  
   ByVal rowNum As Integer _
  
   ByVal backBrush As SystemDrawingBrush _
  
   ByVal foreBrush As SystemDrawingBrush _
  
   ByVal alignToRight As Boolean) 

   Dim text As String 

  text = GetText(GetColumnValueAtRow(source rowNum)) 

   backBrush = New SolidBrush(TextBoxBackColor) 

   foreBrush = New SolidBrush(TextBoxForeColor) 

  ReDim Preserve BackColor(rowcollectionCount) 

   ReDim Preserve ForeColor(rowcollectionCount) 

   Dim i As Integer =  

   Do While (i <= rowcollectionCount) 

   If rowNum = Val(rowcollectionItem(i)) Then 

  If Not BackColor(i )IsEmpty Then 

   backBrush = New SolidBrush(BackColor(i )) 

   End If 

   If Not ForeColor(i )IsEmpty Then 

   foreBrush = New SolidBrush(ForeColor(i )) 

   End If 

   End If 
   
   i +=  

   Loop
   
   MyBasePaintText(g bounds text backBrush foreBrush alignToRight) 
   
   End Sub 
   
   End Class 
   
  關於行顏色定義的類
  
  Imports SystemWindowsForms
  
  Namespace Truck_WEB 
   
   Public Class DrawDGClass 
   
   Public Class ReDrawDataDridControls : Inherits DataGridColoredTextBoxColumn 
   
   Public Sub DrawCorol(ByRef DG As DataGrid Optional ByVal CurrentRowindex As Integer = )
  
   設置選中的行的顏色默認是第一行選中
  
   Dim dt As DataTable
  
   Dim ts As New DataGridTableStyle()
  
   tsAllowSorting = False
  
   Dim aColumnTextColumn As DataGridColoredTextBoxColumn
  
   dt = CType(DGDataSource DataTable)
  
   tsMappingName = CType(DGDataSource DataTable)TableName
  
   DGTableStylesClear()
  
   Dim numCols As Integer
  
   numCols = dtColumnsCount
  
   Dim i j As Integer
  
   i =
  
   j =  

  Do While (i < numCols) 
   
   aColumnTextColumn = New DataGridColoredTextBoxColumn() 
   
   Dim rowindex As Integer =  
   
   For rowindex = To dtRowsCount
  
   Dim StrSel As String
  
   Dim MyForeCorol MyBackCorol As Color
  
   aColumnTextColumnrowcollectionAdd(rowindex)
  
   If rowindex = CurrentRowindex Then
  
   MyForeCorol = ColorWhite
  
   MyBackCorol = ColorDarkSlateBlue
  
   else 
   
   MyForeCorol = ColorDarkSlateBlue
  
   MyBackCorol = ColorWhite
  
   End If 
   
   ReDim Preserve aColumnTextColumnForeColor(aColumnTextColumnrowcollectionCount)
  
   ReDim Preserve aColumnTextColumnBackColor(aColumnTextColumnrowcollectionCount)
  
   aColumnTextColumnForeColor(rowindex) = MyForeCorol
  
   aColumnTextColumnBackColor(rowindex) = MyBackCorol
  
   Next 
   
   要更改列頭名請改下句的HeaderText值
  
   aColumnTextColumnHeaderText = dtColumns(i)ColumnName 
   
   aColumnTextColumnMappingName = dtColumns(i)ColumnName 
   
   tsGridColumnStylesAdd(aColumnTextColumn)
  
   i = (i + )
  
   Loop
  
   DGTableStylesAdd(ts) 
   
   End Sub
  
   End Class
  
   End Class
  
  End Namespace 
   
  以上是設定選中單行的顏色為反色各位還可以借題發揮一下!例如設置顏色等等
  
  在此向《Henry手記:WinForm Datagrid結構剖析》的作者韓睿致謝! 
   
  以後我會盡量完善這個DrawDG的類為大家提供方便!  


From:http://tw.wingwit.com/Article/program/net/201311/12321.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.