D
Dan
Hi all, I'm trying to create a 'smart' equivalent of the
DataSet.HasChanges() method which can tell me if there are no 'true' changes
in a dataset; e.g. when user enters "Alice" in a name field and the old
value was "Alice" too, the DataSet.HasChanges() method rets true but the two
values are equal so that I should not prompt the user to save its changes
when editing such record.
To do this, I was thinking of a function which receives a dataset and cycles
through all the tables and (changed) rows fields comparing the original
version with the current version to see if they are actually different.
Anyway, how can I implement in a general way comparison between two fields
without knowing in advance their type? I could compare the reference for
row[i, DataRowVersion.Current] with that of row[i, DataRowVersion.Original]
to see if they are equal (which means that their value is the same), but
this does not work in all cases (e.g. it works for strings but not for
integers). Could anyone give a hint? A sample codepiece follows...
Thanks!
===================================
public bool SmartHasChanges(DataSet ds)
{
if (!ds.HasChanges()) return false;
foreach (DataTable dt in ds.Tables)
{
DataTable dtChanges = dt.GetChanges();
if (dtChanges != null)
{
foreach (DataRow row in dtChanges.Rows)
{
switch (row.RowState)
{
case DataRowState.Added:
return true;
case DataRowState.Deleted:
return true;
case DataRowState.Modified:
DumpVersions(row);
for (int i = 0; i < dt.Columns.Count; i++)
{
if ( (row.HasVersion(DataRowVersion.Current)) &&
(row[i, DataRowVersion.Current] !=
row[i, DataRowVersion.Original]) )
return true;
}
break;
}
}
}
}
return false;
}
DataSet.HasChanges() method which can tell me if there are no 'true' changes
in a dataset; e.g. when user enters "Alice" in a name field and the old
value was "Alice" too, the DataSet.HasChanges() method rets true but the two
values are equal so that I should not prompt the user to save its changes
when editing such record.
To do this, I was thinking of a function which receives a dataset and cycles
through all the tables and (changed) rows fields comparing the original
version with the current version to see if they are actually different.
Anyway, how can I implement in a general way comparison between two fields
without knowing in advance their type? I could compare the reference for
row[i, DataRowVersion.Current] with that of row[i, DataRowVersion.Original]
to see if they are equal (which means that their value is the same), but
this does not work in all cases (e.g. it works for strings but not for
integers). Could anyone give a hint? A sample codepiece follows...
Thanks!
===================================
public bool SmartHasChanges(DataSet ds)
{
if (!ds.HasChanges()) return false;
foreach (DataTable dt in ds.Tables)
{
DataTable dtChanges = dt.GetChanges();
if (dtChanges != null)
{
foreach (DataRow row in dtChanges.Rows)
{
switch (row.RowState)
{
case DataRowState.Added:
return true;
case DataRowState.Deleted:
return true;
case DataRowState.Modified:
DumpVersions(row);
for (int i = 0; i < dt.Columns.Count; i++)
{
if ( (row.HasVersion(DataRowVersion.Current)) &&
(row[i, DataRowVersion.Current] !=
row[i, DataRowVersion.Original]) )
return true;
}
break;
}
}
}
}
return false;
}