NBA

VB.NET中关于DataGrid颜色的自定义

2019-09-12 11:26:06来源:励志吧0次阅读

介绍了WIN DG的颜色的定义,但是主要是针对每一个Cell的。

我们需要的则是标记某一行的数据,用颜色突出显示,所以作了部分改动,现在把部分代码张贴出来供大家参考:

1. 基础类出自韩睿:

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

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 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))

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

Loop

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.MappingName = 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

治疗血栓的食物
丁桂薏芽健脾凝胶成分
心肌梗塞前期的治疗
小儿肠痉挛腹痛症状有什么
分享到: