Public Class DataGridColoredTextBoxColumn
Inherits DataGridTextBoxColumn '继承接口
Public rowcollection As New Collection '用于存放行号的集合
Public BackColor() As Color '用于存放背景色的集合,索引与行号集号一致(集合索引减1)
Public ForeColor() As Color '用于存放前景色的集合,索引与行号集号一致
Private Function GetText(ByVal Value As Object) As String
'为进行重绘读出textbox中的内容,这个函数是增加容错能力的,与实现机制无关
If TypeOf (Value) Is System.DBNull Then
Return NullText
ElseIf Value Is Nothing Then
Return ""
Else
Return Value.ToString
End If
End Function
Protected Overloads Overrides 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 System.Drawing.Brush, _
ByVal foreBrush As System.Drawing.Brush, _
ByVal alignToRight As Boolean)
Dim text As String
'按默认模式重绘
text = GetText(GetColumnValueAtRow(source, rowNum)) '这句就是上文提及的取数据
If text = "(null)" Then
text = ""
End If
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 '集合索引从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
Loop
MyBase.PaintText(g, bounds, text, backBrush, foreBrush, alignToRight)
End Sub
End Class
Do While (i < numCols) '重绘所有的列
aColumnTextColumn = New DataGridColoredTextBoxColumn
j = 0
If S_count > 0 Then
For j = 0 To StopDe.Length - 1
aColumnTextColumn.rowcollection.Add(StopDe(j))
ReDim Preserve aColumnTextColumn.ForeColor(aColumnTextColumn.rowcollection.Count)
ReDim Preserve aColumnTextColumn.BackColor(aColumnTextColumn.rowcollection.Count)
aColumnTextColumn.ForeColor(j) = Color.Gray
aColumnTextColumn.BackColor(j) = Color.White
Next
End If
If E_count > 0 Then
For m = 0 To EndDe.Length - 1
aColumnTextColumn.rowcollection.Add(EndDe(m))
ReDim Preserve aColumnTextColumn.ForeColor(aColumnTextColumn.rowcollection.Count)
ReDim Preserve aColumnTextColumn.BackColor(aColumnTextColumn.rowcollection.Count)
aColumnTextColumn.ForeColor(j) = Color.Red
aColumnTextColumn.BackColor(j) = Color.White
j = j + 1
Next
End If
aColumnTextColumn.HeaderText = dvd.Table.Columns(i).ColumnName
aColumnTextColumn.MappingName = dvd.Table.Columns(i).ColumnName
aColumnTextColumn.Alignment = HorizontalAlignment.Center
ts.GridColumnStyles.Add(aColumnTextColumn)
i = (i + 1)
Loop
Me.DgdDelegaInfo.TableStyles.Clear()
Me.DgdDelegaInfo.TableStyles.Add(ts) 'datagrid