How to change color of dirty cells?

ppsa

Member
Joined
Sep 28, 2008
Messages
13
Programming Experience
10+
I have a DataGridView bound to a DataTable. When the user changes a value in a cell, I'd like to change the background color of that cell. If the user changes the value back to the original, saved value, I'd like to change the color back to the original.

I tried to implement this will a nested loop in the CellValueChanged event handler of the grid that compares the cells of the datagridview with the "cells" in the datatable, like this (CATColumns() is an array that contains the column names alligned with the datatable (and grid)):

VB.NET:
        For r As Integer = 0 To DT.Rows.Count - 1
            For c As Integer = 0 To DT.Columns.Count - 1
                IF DT.Rows(r).Item(CATColumns(c)).ToString() <> DGV.Item(c, r).Value.ToString() Then
                     DGV.Item(c, r).Style.BackColor = Color.Yellow
                Else
                     DGV.Item(c, r).Style.BackColor = Color.White
                End If
            Next
        Next

However, the color didn't change as I made changes. When I changed the code to this:

VB.NET:
        For r As Integer = 0 To DT.Rows.Count - 1
            For c As Integer = 0 To DT.Columns.Count - 1
                MsgBox(DT.Rows(r).Item(CATColumns(c)).ToString() & "|" & DGV.Item(c, r).Value.ToString())
            Next
        Next

I noticed, much to my surprise, that there is never any difference between the values in the grid and the ones in the datatable! How do I implement what I'm trying to do?

Thanks!
 
Top Bottom