近来项目用到了一个类似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的类为大家提供方便!