Hier der Code:
Public Class MyDataGridTextBoxColumn
Inherits DataGridTextBoxColumn
Private Const cMinHeight = 16
Private mBackcolor As Color
Private mForeColor As Color
Private mAlternatingBackColor As Color
Private mAlternatingForeColor As Color
Private mMultiLine As Boolean
Private mShowButton As Boolean
Private mEdit As Boolean
Private mSetText As Boolean
Private mValidateError As Boolean
Private WithEvents mDGrid As MyGrid
Private WithEvents mTextBox As MyTextBox
Public Event BeforeColumnUpdate(ByVal Row As Integer, ByVal Col As Integer, _
ByRef Value As Object, ByRef Cancel As Boolean)
Public Event TextChanged(ByVal sender As Object, ByVal Value As String)
Public Sub New()
MyBase.New()
InitTextBox()
End Sub
' BackColor (Hintergrundfarbe der Spalte)
Public Property BackColor() As Color
Get
BackColor = mBackcolor
End Get
Set(ByVal Value As Color)
mBackcolor = Value
Me.Invalidate()
End Set
End Property
' ForeColor (Vordergrundfarbe der Spalte)
Public Property ForeColor() As Color
Get
ForeColor = mForeColor
End Get
Set(ByVal Value As Color)
mForeColor = Value
Me.Invalidate()
End Set
End Property
' AlternatingBackColor (alternierende Hintergrundfarbe der Spalte)
Public Property AlternatingBackColor() As Color
Get
AlternatingBackColor = mAlternatingBackColor
End Get
Set(ByVal Value As Color)
mAlternatingBackColor = Value
End Set
End Property
' AlternatingForeColor (alternierende Vordergrundfarbe der Spalte)
Public Property AlternatingForeColor() As Color
Get
AlternatingForeColor = mAlternatingForeColor
End Get
Set(ByVal Value As Color)
mAlternatingForeColor = Value
End Set
End Property
' Multiline zulassen
Public Property MultiLine() As Boolean
Get
MultiLine = mMultiLine
End Get
Set(ByVal Value As Boolean)
mMultiLine = Value
mTextBox.Multiline = mMultiLine
End Set
End Property
' Textbox-Objekt auf modifizierte Textbox setzen
Public Overrides ReadOnly Property TextBox() As System.Windows.Forms.TextBox
Get
TextBox = mTextBox
End Get
End Property
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 VarRect As RectangleF
Dim VarSize As SizeF
mDGrid = Me.DataGridTableStyle.DataGrid
' Bei selektierten Zeilen
If mDGrid.IsSelected(rowNum) Then
backBrush = New SolidBrush(mDGrid.SelectionBackColor)
foreBrush = New SolidBrush(mDGrid.SelectionForeColor)
Else
If mBackcolor.Equals(Color.Empty) Then
mBackcolor = mDGrid.BackColor
End If
If mForeColor.Equals(Color.Empty) Then
mForeColor = mDGrid.ForeColor
End If
If mAlternatingBackColor.Equals(Color.Empty) Then
' Alle Zeilen gleiche Hintergrundfarbe
backBrush = New SolidBrush(mBackcolor)
Else
' Zeilen alternierende Hintergrundfarbe
If (rowNum Mod 2) = 0 Then
backBrush = New SolidBrush(mBackcolor)
Else
backBrush = New SolidBrush(mAlternatingBackColor)
End If
End If
If mAlternatingForeColor.Equals(Color.Empty) Then
' alle Zeilen gleich Vordergrundfarbe
foreBrush = New SolidBrush(mForeColor)
Else
' Zeilen alternierende Vordergrundfarbe
If (rowNum Mod 2) = 0 Then
foreBrush = New SolidBrush(mForeColor)
Else
foreBrush = New SolidBrush(mAlternatingForeColor)
End If
End If
End If
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
End Sub |