ADO.NET: Is my data really dirty?
Example: User changes the value in a databound control. Users changes the value back to it's original value. User initiates a Commit\Save.
Should the save go through?
No in my opinion. The data has not 'really' changed from the user's point of view.
If you are using the ADO.NET update model though, the DataRow gets marked as Modified.
How to 'prevent' this? You can't unless you call RejectChanges on the DataRow.
So, write yourself a DataDirty method to check each column's current value and compare it to the original value. If they are not different for all columns in the DataRow, RejectChanges.
Public Function DataReallyIsDirty(ByVal dt As DataTable) As Boolean
Dim result As Boolean = False
For Each drw As DataRow In dt.Rows
If drw.RowState = DataRowState.Modified Then
Dim curr, orig As String
For Each col As DataColumn In dt.Columns
Try
curr = drw(col, DataRowVersion.Current).ToString().Trim()
orig = drw(col, DataRowVersion.Original).ToString().Trim()
If (Not curr.Equals(orig) OrElse _
curr <> orig OrElse _
String.CompareOrdinal(curr, orig) <> 0) AndAlso _
Not (curr = "-" AndAlso orig = String.Empty) Then
result = True
End If
Catch
Throw
End Try
Next
Else
If drw.RowState = DataRowState.Added Then Return True
End If
Next
Return result
End Function ' DataReallyIsDirty