M
Mad Scientist Jr
Can someone post a clear example of how to change the background color
of an individual datagrid cell for a Windows desktop app? (preferably
in vb.net)
I found some code on how to do this when the user clicks on the cell:
Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
DataGrid1.Controls(DataGrid1.CurrentCell.ColumnNumber +
2).BackColor = System.Drawing.Color.Blue
DataGrid1.Controls(DataGrid1.CurrentCell.ColumnNumber +
2).ForeColor = System.Drawing.Color.White
End Sub
I tried setting this property outside the event, but the color isn't
visible until the cell is clicked on:
Dim DataGridCell2 As New DataGridCell
DataGridCell2.ColumnNumber = 1
DataGridCell2.RowNumber = 1
DataGrid1.Controls(DataGridCell2.ColumnNumber + 2).BackColor =
System.Drawing.Color.Blue
DataGrid1.Controls(DataGridCell2.ColumnNumber + 2).ForeColor =
System.Drawing.Color.White
I found a post with code, saying you have to override the paint event.
I tried pasting this in my project, but haven't gotten it to work (see
below)
Any help appreciated !
Public Class DataGridColoredTextBoxColumn
Inherits DataGridTextBoxColumn
Private m_backColor As Color
Private m_foreColor As Color
Public Property BackColor() As Color
Get
If m_backColor.IsEmpty Then
m_backColor = DataGridTableStyle.BackColor
End If
Return m_backColor
End Get
Set(ByVal Value As Color)
m_backColor = Value
End Set
End Property
Public Property ForeColor() As Color
Get
If m_foreColor.IsEmpty Then
m_foreColor = DataGridTableStyle.ForeColor
End If
Return m_foreColor
End Get
Set(ByVal Value As Color)
m_foreColor = Value
End Set
End Property
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)
MyClass.Paint(g, bounds, source, rowNum, False)
End Sub
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 alignToRight As
Boolean)
Dim backBrush As Brush
Dim foreBrush As Brush
backBrush = New SolidBrush(BackColor)
foreBrush = New SolidBrush(ForeColor)
MyClass.Paint(g, bounds, source, rowNum, backBrush,
foreBrush, alignToRight)
End Sub
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(BackColor)
foreBrush = New SolidBrush(ForeColor)
MyBase.PaintText(g, bounds, text, backBrush, foreBrush,
alignToRight)
End Sub
Public Overrides Property [ReadOnly]() As Boolean
Get
Return True
End Get
Set(ByVal Value As Boolean)
Throw New NotSupportedException("ReadOnly property is
not changable!")
End Set
End Property
End Class
of an individual datagrid cell for a Windows desktop app? (preferably
in vb.net)
I found some code on how to do this when the user clicks on the cell:
Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
DataGrid1.Controls(DataGrid1.CurrentCell.ColumnNumber +
2).BackColor = System.Drawing.Color.Blue
DataGrid1.Controls(DataGrid1.CurrentCell.ColumnNumber +
2).ForeColor = System.Drawing.Color.White
End Sub
I tried setting this property outside the event, but the color isn't
visible until the cell is clicked on:
Dim DataGridCell2 As New DataGridCell
DataGridCell2.ColumnNumber = 1
DataGridCell2.RowNumber = 1
DataGrid1.Controls(DataGridCell2.ColumnNumber + 2).BackColor =
System.Drawing.Color.Blue
DataGrid1.Controls(DataGridCell2.ColumnNumber + 2).ForeColor =
System.Drawing.Color.White
I found a post with code, saying you have to override the paint event.
I tried pasting this in my project, but haven't gotten it to work (see
below)
Any help appreciated !
Public Class DataGridColoredTextBoxColumn
Inherits DataGridTextBoxColumn
Private m_backColor As Color
Private m_foreColor As Color
Public Property BackColor() As Color
Get
If m_backColor.IsEmpty Then
m_backColor = DataGridTableStyle.BackColor
End If
Return m_backColor
End Get
Set(ByVal Value As Color)
m_backColor = Value
End Set
End Property
Public Property ForeColor() As Color
Get
If m_foreColor.IsEmpty Then
m_foreColor = DataGridTableStyle.ForeColor
End If
Return m_foreColor
End Get
Set(ByVal Value As Color)
m_foreColor = Value
End Set
End Property
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)
MyClass.Paint(g, bounds, source, rowNum, False)
End Sub
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 alignToRight As
Boolean)
Dim backBrush As Brush
Dim foreBrush As Brush
backBrush = New SolidBrush(BackColor)
foreBrush = New SolidBrush(ForeColor)
MyClass.Paint(g, bounds, source, rowNum, backBrush,
foreBrush, alignToRight)
End Sub
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(BackColor)
foreBrush = New SolidBrush(ForeColor)
MyBase.PaintText(g, bounds, text, backBrush, foreBrush,
alignToRight)
End Sub
Public Overrides Property [ReadOnly]() As Boolean
Get
Return True
End Get
Set(ByVal Value As Boolean)
Throw New NotSupportedException("ReadOnly property is
not changable!")
End Set
End Property
End Class