前几日,搜了好久有关vb.net中DataGrid指定行颜色设置的相关信息,拜读了很多力作。稍微修改了以下,用了以下还不错。
Dim
arrList
As
New
ArrayList
For i As Integer = 0 To mytable.Rows.Count - 1
If CInt (mytable.Rows(i)( " Bladder_Max " )) > CInt (mytable.Rows(i)( " Bladder_Count " )) Then
arrList.Add( 0 )
Else
arrList.Add( 1 )
End If
Next
Dim TextCol As DataGridColoredTextBoxColumn
TextCol = New DataGridColoredTextBoxColumn(arrList, mytable.Rows.Count, Color.White, Color.Red)
TextCol.MappingName = " Bladder_Name "
TextCol.HeaderText = " Bladder名称 "
TextCol.Alignment = System.Windows.Forms.HorizontalAlignment.Center
TextCol.Width = Me .DataGrid1.Width / 6 - 10
TextCol.TextBox.Enabled = False
ts1.GridColumnStyles.Add(TextCol)
' ........
Me .DataGrid1.TableStyles.Clear()
Me .DataGrid1.TableStyles.Add(ts1)
For i As Integer = 0 To mytable.Rows.Count - 1
If CInt (mytable.Rows(i)( " Bladder_Max " )) > CInt (mytable.Rows(i)( " Bladder_Count " )) Then
arrList.Add( 0 )
Else
arrList.Add( 1 )
End If
Next
Dim TextCol As DataGridColoredTextBoxColumn
TextCol = New DataGridColoredTextBoxColumn(arrList, mytable.Rows.Count, Color.White, Color.Red)
TextCol.MappingName = " Bladder_Name "
TextCol.HeaderText = " Bladder名称 "
TextCol.Alignment = System.Windows.Forms.HorizontalAlignment.Center
TextCol.Width = Me .DataGrid1.Width / 6 - 10
TextCol.TextBox.Enabled = False
ts1.GridColumnStyles.Add(TextCol)
' ........
Me .DataGrid1.TableStyles.Clear()
Me .DataGrid1.TableStyles.Add(ts1)
Public
Class DataGridColoredTextBoxColumntBoxColumn
Class DataGridColoredTextBoxColumntBoxColumn
Private m_Row As Integer = -1
Private m_BackColor As Color = Color.White
Private m_ForeColor As Color = Color.Black
Private defalutBack As Brush = New SolidBrush(Color.White)
Private defalutFore As Brush = New SolidBrush(Color.Black)
Private m_Smt As Integer
Private m_Array As ArrayList
Sub New()Sub New(ByVal Row As Integer, ByVal [ForeColor] As Color)
m_Row = Row
m_ForeColor = ForeColor
End Sub
Sub New()Sub New(ByVal row As Integer, ByVal [ForeColor] As Color, ByVal [BackColor] As Color)
m_Row = row
m_ForeColor = ForeColor
m_BackColor = BackColor
End Sub
Sub New()Sub New(ByVal row As Integer, ByVal cnt As Integer, ByVal [ForeColor] As Color, ByVal [BackColor] As Color)
m_Row = row
m_Smt = cnt
m_ForeColor = ForeColor
m_BackColor = BackColor
End Sub
Sub New()Sub New(ByVal arrList As ArrayList, ByVal cnt As Integer, ByVal [ForeColor] As Color, ByVal [BackColor] As Color)
m_Row = -1
m_Array = arrList
m_Smt = cnt
m_ForeColor = ForeColor
m_BackColor = BackColor
End Sub
Protected Overloads Overrides Sub Paint()Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
Try
If rowNum < m_Smt Then
If m_Array(rowNum) = 0 Then
Else
backBrush = New SolidBrush(m_BackColor)
foreBrush = New SolidBrush(m_ForeColor)
End If
End If
Catch ex As Exception
' empty catch
Finally
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
End Try
End Sub
End Class
Private m_Row As Integer = -1
Private m_BackColor As Color = Color.White
Private m_ForeColor As Color = Color.Black
Private defalutBack As Brush = New SolidBrush(Color.White)
Private defalutFore As Brush = New SolidBrush(Color.Black)
Private m_Smt As Integer
Private m_Array As ArrayList
Sub New()Sub New(ByVal Row As Integer, ByVal [ForeColor] As Color)
m_Row = Row
m_ForeColor = ForeColor
End Sub
Sub New()Sub New(ByVal row As Integer, ByVal [ForeColor] As Color, ByVal [BackColor] As Color)
m_Row = row
m_ForeColor = ForeColor
m_BackColor = BackColor
End Sub
Sub New()Sub New(ByVal row As Integer, ByVal cnt As Integer, ByVal [ForeColor] As Color, ByVal [BackColor] As Color)
m_Row = row
m_Smt = cnt
m_ForeColor = ForeColor
m_BackColor = BackColor
End Sub
Sub New()Sub New(ByVal arrList As ArrayList, ByVal cnt As Integer, ByVal [ForeColor] As Color, ByVal [BackColor] As Color)
m_Row = -1
m_Array = arrList
m_Smt = cnt
m_ForeColor = ForeColor
m_BackColor = BackColor
End Sub
Protected Overloads Overrides Sub Paint()Sub Paint(ByVal g As System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, ByVal source As System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
Try
If rowNum < m_Smt Then
If m_Array(rowNum) = 0 Then
Else
backBrush = New SolidBrush(m_BackColor)
foreBrush = New SolidBrush(m_ForeColor)
End If
End If
Catch ex As Exception
' empty catch
Finally
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
End Try
End Sub
End Class