editing datagrid and changing cell color upon edit
I am using multiple datagrids in the same form and would like any cell that is editable to change colors upon edit. I have found some code using a ColoredTextBoxColumn that inherits DataGridTextBoxColumn. This will change cell colors when a cell doesn't meet the cutoff value. I need it to change colors when edited. Here is the code that i have came up with so far.
Public Class ColoredTextBoxColumn
Inherits DataGridTextBoxColumn
Private m_Cutoff As Single = 0
Public Sub New(ByVal cutoff As Single)
m_Cutoff = cutoff
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 foreBrush As Brush, ByVal backBrush As Brush, ByVal alignToRight As Boolean)
'get the cell's value
Dim cell_object As Object
Dim checkCol, checkRow As Object
Dim curcell As New DataGridCell
Dim j, k As Integer
Dim iselected As String
Dim ItemsInfo As DataTable
Dim itm2 As cDieType
Dim items2 As Collection
Dim curItem As Object
cell_object = Me.GetColumnValueAtRow(source, rowNum)
'if the value is numeric, get its value
Dim cell_number As Double '= m_Cutoff - 1
If IsNumeric(cell_object) Then
cell_number = CType(cell_object, Double)
End If
ItemsInfo = Me.DataGridTableStyle.DataGrid.DataSource()
For k = 0 To ItemsInfo.Rows.Count - 1
If iselected = Me.DataGridTableStyle.DataGrid.Item(j, 0) Then
j = j
Exit For
Else
j = j + 1
End If
Next
curcell.RowNumber = j
curcell.ColumnNumber = 0
'Me.DataGridTableStyle.DataGrid.CurrentCell = curcell
'Me.DataGridTableStyle.DataGrid.Select(j)
'pick appropriate colors
If (cell_number <> m_Cutoff) Then 'm_Cutoff
backBrush = Brushes.Pink
foreBrush = Brushes.Red
Else
backBrush = Brushes.White
foreBrush = Brushes.Black
End If
'Draw the cell
MyBase.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight)
End Sub
End Class
|