Wednesday, June 10, 2009

TechNotes: Cell Coloring for Modified Cells in DataGridView

Using Visual Studio 2008, here is an example of updating cell coloring in a datagrid to highlight the cells containing modifications.

VB.Net,Visual Studio 2008,DataGridView,CellColoring

Private Sub grdTestData_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdTestData.CellEndEdit
Dim view As DataView = CType(Me.grdTestData.DataSource, DataView)
Dim dataViewRow As DataRow = view.Item(e.RowIndex).Row
Dim Day1 As DataGridViewCell = Me.grdTestData("Day1", e.RowIndex)
Dim Day2 As DataGridViewCell = Me.grdTestData("Day2", e.RowIndex)
Dim Day3 As DataGridViewCell = Me.grdTestData("Day3", e.RowIndex)
Dim Day4 As DataGridViewCell = Me.grdTestData("Day4", e.RowIndex)
Dim Day5 As DataGridViewCell = Me.grdTestData("Day5", e.RowIndex)
Dim Day6 As DataGridViewCell = Me.grdTestData("Day6", e.RowIndex)
Dim Day7 As DataGridViewCell = Me.grdTestData("Day7", e.RowIndex)
Dim originalValueDay1 As Object = dataViewRow.Item("Day1_Yds", DataRowVersion.Original)
Dim originalValueDay2 As Object = dataViewRow.Item("Day2_Yds", DataRowVersion.Original)
Dim originalValueDay3 As Object = dataViewRow.Item("Day3_Yds", DataRowVersion.Original)
Dim originalValueDay4 As Object = dataViewRow.Item("Day4_Yds", DataRowVersion.Original)
Dim originalValueDay5 As Object = dataViewRow.Item("Day5_Yds", DataRowVersion.Original)
Dim originalValueDay6 As Object = dataViewRow.Item("Day6_Yds", DataRowVersion.Original)
Dim originalValueDay7 As Object = dataViewRow.Item("Day7_Yds", DataRowVersion.Original)

If originalValueDay1.Equals(Day1.Value) Then
If Day1.Style.BackColor = Drawing.Color.Red Then
Day1.Style.BackColor = Me.grdTestData(0, e.RowIndex).Style.BackColor
End If
Day1.Style.BackColor = Drawing.Color.Red
End If

If originalValueDay2.Equals(Day2.Value) Then
If Day2.Style.BackColor = Drawing.Color.Red Then
Day2.Style.BackColor = Me.grdTestData(0, e.RowIndex).Style.BackColor
End If
Day2.Style.BackColor = Drawing.Color.Red
End If

If originalValueDay3.Equals(Day3.Value) Then
If Day3.Style.BackColor = Drawing.Color.Red Then
Day3.Style.BackColor = Me.grdTestData(0, e.RowIndex).Style.BackColor
End If
Day3.Style.BackColor = Drawing.Color.Red
End If

If originalValueDay4.Equals(Day4.Value) Then
If Day4.Style.BackColor = Drawing.Color.Red Then
Day4.Style.BackColor = Me.grdTestData(0, e.RowIndex).Style.BackColor
End If
Day4.Style.BackColor = Drawing.Color.Red
End If

If originalValueDay5.Equals(Day5.Value) Then
If Day5.Style.BackColor = Drawing.Color.Red Then
Day5.Style.BackColor = Me.grdTestData(0, e.RowIndex).Style.BackColor
End If
Day5.Style.BackColor = Drawing.Color.Red
End If

If originalValueDay6.Equals(Day6.Value) Then
If Day6.Style.BackColor = Drawing.Color.Red Then
Day6.Style.BackColor = Me.grdTestData(0, e.RowIndex).Style.BackColor
End If
Day6.Style.BackColor = Drawing.Color.Red
End If

If originalValueDay7.Equals(Day7.Value) Then
If Day7.Style.BackColor = Drawing.Color.Red Then
Day7.Style.BackColor = Me.grdTestData(0, e.RowIndex).Style.BackColor
End If
Day7.Style.BackColor = Drawing.Color.Red
End If
End Sub
I know there is still room for refactoring the above code, but this is a simple solution for updating the cell color on changed values.

No comments: