title: Revert DataGridView cell contents to original value
body: <p>(<strong>I have rephrased this entire question in an attempt to be more clear in what I'm trying to do</strong>)</p>
  
  <p>This is a VB.Net WinForms project being written in VS2012.</p>
  
  <p>When the user changes the contents of a DataGridView's cell, I am changing the background color of the cell to visually indicate to the user which cells they have changed. That works fine.</p>
  
  <p>However, if the user changes the cell contents back to what the value was before they initially edited it (essentially what the database table's value is), I need to change the background color to its default row background color.</p>
  
  <p>So, in the CellValidating event I am using the following lines to get the value from the DataGridView's bound dataset and the value that the user entered so I can compare the two:</p>
  
  <pre><code>Dim dgvColDataName As String = dgvEmployees.Columns(e.ColumnIndex).DataPropertyName
  Dim dgvRowID As Integer = CInt(dgvEmployees.Rows(e.RowIndex).Cells(1).Value)
  Dim index As Integer = EmployeesDataSet.Tables("employees").Rows.IndexOf(EmployeesDataSet.Tables("employees").Rows().Find(dgvRowID))
  Dim originalValue As String = EmployeesDataSet.Tables("employees").Rows(index).Item(dgvColDataName).ToString()
  Dim enteredValue As String = dgvEmployees.Rows(e.RowIndex).Cells(e.ColumnIndex).EditedFormattedValue.ToString()
  </code></pre>
  
  <p>These get the correct, corresponding values between the DataGridView's current cell and its corresponding cell in the bound table.</p>
  
  <p>Here is where I am comparing the values to determine if the value entered is the same as the original dataset's value:</p>
  
  <pre><code>If originalValue.ToUpper() &lt;&gt; enteredValue.ToString().ToUpper() Then
      ' Color the edited cell's background
      dgvEmployees.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.LightPink
  Else
      ' Set the back color to its default style
      dgvEmployees.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = dgvEmployees.Rows(e.RowIndex).Cells(e.ColumnIndex).InheritedStyle.BackColor
  End If
  </code></pre>
  
  <p>I thought this was the answer. The cell background is being changed when the value is changed. But I was surprised to discover that if the cell loses focus (leaves Edit Mode) and then the user clicks back in that cell and changes the value to what it originally was before initially editing it, the cell back color doesn't change back to the default row/cell color. I.e. apparently when the cell loses focus, the dataset is changed, so <code>originalValue</code> is now the last entered value.</p>
  
  <p>I need to be able to get the value from when the DataGridView was first loaded or after the last save.</p>
  
  <p>Any ideas?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>After some more research I was able to figure out how to access the DataSet's original, pre-editing value of a given cell.</p>
  
  <p>In the variable assignment code above I added this line to get the relevant DataSet row:</p>
  
  <pre><code>Dim row As DataRow = EmployeesDataSet.Tables("employees").Rows(dsRowIndex)
  </code></pre>
  
  <p>Then I replaced this line:</p>
  
  <pre><code>Dim originalValue As String = EmployeesDataSet.Tables("employees").Rows(index).Item(dgvColDataName).ToString()
  </code></pre>
  
  <p>with this:</p>
  
  <pre><code>Dim originalValue As String = row(dgvColDataName, DataRowVersion.Original).ToString()
  </code></pre>
  
  <p>So after thoroughly testing this I am confident that, regardless of the state of the DGV, editing controls, etc. the <code>originalValue</code> will be the value that came from the database table.</p>
  
  <p>I'm marking this as answered...</p>