Home All Groups Group Topic Archive Search About

Retrieve only changed values from a DataRow ?

Author
8 Mar 2006 12:33 PM
Rune B
- I need to find the columnNames of values that has Changed in a certain
DataRow, and I can't quite figure out how to do it right ... The following
does seems a little clumsy, and does not work right either.

Anybody knows how to do it?


private IList<string> GetChangedFieldsColumnNames(DataRow row)
{
List<string> result = new List<string>();
if(row != null)
{
  switch(row.RowState)
  {
   case DataRowState.Detached:
    break;
   case DataRowState.Modified:
   case DataRowState.Added:
    foreach(DataColumn column in row.Table.Columns)
    {
     try
     {
      object originalValue = row[column, DataRowVersion.Original];
      object newValue = row[column, DataRowVersion.Current];
      if(originalValue != newValue)
      {
       result.Add(column.ColumnName);
      }
     }
     catch(VersionNotFoundException)
     {
     }
    }
    break;
   case DataRowState.Deleted:
   case DataRowState.Unchanged:
    break;
  }
}
return result.ToArray();
}

Author
9 Mar 2006 8:16 PM
serge calderara
Hi,

you could retrive in a daatset object only records which have change with
the GetChanges function of a dataset object.
Then creat a datview object by filtering only rowstate

hope it help
serge

Show quote
"Rune B" wrote:

>
> - I need to find the columnNames of values that has Changed in a certain
> DataRow, and I can't quite figure out how to do it right ... The following
> does seems a little clumsy, and does not work right either.
>
> Anybody knows how to do it?
>
>
> private IList<string> GetChangedFieldsColumnNames(DataRow row)
> {
>  List<string> result = new List<string>();
>  if(row != null)
>  {
>   switch(row.RowState)
>   {
>    case DataRowState.Detached:
>     break;
>    case DataRowState.Modified:
>    case DataRowState.Added:
>     foreach(DataColumn column in row.Table.Columns)
>     {
>      try
>      {
>       object originalValue = row[column, DataRowVersion.Original];
>       object newValue = row[column, DataRowVersion.Current];
>       if(originalValue != newValue)
>       {
>        result.Add(column.ColumnName);
>       }
>      }
>      catch(VersionNotFoundException)
>      {
>      }
>     }
>     break;
>    case DataRowState.Deleted:
>    case DataRowState.Unchanged:
>     break;
>   }
>  }
>  return result.ToArray();
> }
>
>
>
>

AddThis Social Bookmark Button