title: Detect rows in DataGridView whose cell values have been changed by user
body: <p>I have a VB.NET WinForms project that I'm building in VS2013. In a DataGridView bound to a DataSet I want to highlight each row that the user has changed - and if the user changes a row's contents back to its original values (as compared to the database values) I want to remove the highlighting.</p>
  
  <p>I have been Googling for a while now and really haven't gotten anywhere.</p>
  
  <p>All I know at this point is that <code>EmployeesDataSet.HasChanges(DataRowState.Modified)</code> returns False in the CellValueChanged event after having changed text in a cell and clicked out of the row.</p>
  
  <p>My assumption is that the overall method would be something like on KeyUp event compare the current row's cell values to the DataSet (or BindingSource or TableAdapter?) and if anything is different, highlight the row, otherwise set the row to the default backcolor.</p>
  
  <p>But if that's the right approach I don't understand what I would compare the row's contents to. Would it be the DataSet? The TableAdapter? The BindingSource? If it's one of those, how to I compare the correct row?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Some more research has made some progress:</p>
  
  <p>I found this iteration code:</p>
  
  <pre><code>Dim dsChanged As DataSet = EmployeesDataSet.GetChanges()
  
  For Each dt As DataTable In dsChanged.Tables
      For Each row As DataRow In dt.Rows
          For i As Integer = 0 To dt.Columns.Count - 1
              Dim currentBackColor As System.Drawing.Color = dgvEmployees.AlternatingRowsDefaultCellStyle.BackColor
              If Not row(i, DataRowVersion.Current).Equals(row(i, DataRowVersion.Original)) Then
                  dgvEmployees.Rows(dt.Rows.IndexOf(row)).DefaultCellStyle.BackColor = Color.LightPink
              Else
                  ' No changes so set it to its original color
                  dgvEmployees.Rows(dt.Rows.IndexOf(row)).DefaultCellStyle.BackColor = currentBackColor
              End If
          Next
      Next
  Next
  </code></pre>
  
  <p>I put this in a separate Sub, which is being called in the DataGridView.CellValueChanged event.</p>
  
  <p>That correctly detects the rows that have changed cell values, but my code to color the background isn't quite right. As is, it is coloring each successive row from top to bottom as I make changes - regardless of what row in the DGV I edit.</p>
  
  <p>I assumed that dt.Rows.IndexOf(row) would correctly get the correct index of the DGV, since I'm iterating through the DGV's DataTable.</p>