M
matt
hello,
i have two datasets (only one DataTable each) representing the same set
of data, but from two different historic points of time. so they are of
the same structure, but there may have been rows added, removed, or
changed. each row has a unique ID column. i need a way to show my users
the row differences between the two. (much like using Visual Source
Safe's "Show Differences" on a local vs server versions of a file)
i read with glee that there is built-in support for getting the
differences between datasets. great! i read its done like so:
//get the two datasets data
DataSet historic = GetHistoricData();
DataSet current = GetCurrentData();
//make merge container
DataSet merged = new DataSet();
//seed w/ orig data
merged.Merge(historic);
merged.AcceptChanges();
//merge w/ new data
merged.Merge(current);
//and get the differences
DataSet added = merged.GetChanges(DataRowState.Added);
DataSet deleted = merged.GetChanges(DataRowState.Deleted);
DataSet modified = merged.GetChanges(DataRowState.Modified);
....however, that doesnt *quite* seem to work for me. in the above, only
dataset "added" has data, and the data in it is the exactly whats in
dataset "current".
here are the functions that make some test data:
public static DataSet GetHistoricData()
{
DataTable dt = new DataTable();
dt.Columns.Add("PersonID", System.Type.GetType("System.Int32"));
dt.Columns.Add("FirstName", System.Type.GetType("System.String"));
dt.Columns.Add("LastName", System.Type.GetType("System.String"));
dt.Columns.Add("Comments", System.Type.GetType("System.String"));
dt.Rows.Add( new Object[4] { 66, "matt", "smith", "shipped to
office" } );
dt.Rows.Add( new Object[4] { 67, "jonnie", "tyler", "shipped to
home" } );
dt.Rows.Add( new Object[4] { 68, "cory", "default", "shipped to
home" } );
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds;
}
//''''''''''''''''''''''''''''''''''''''''''''''''''''
public static DataSet GetCurrentData()
{
DataTable dt = new DataTable();
dt.Columns.Add("PersonID", System.Type.GetType("System.Int32"));
dt.Columns.Add("FirstName", System.Type.GetType("System.String"));
dt.Columns.Add("LastName", System.Type.GetType("System.String"));
dt.Columns.Add("Comments", System.Type.GetType("System.String"));
//row stayed the same:
dt.Rows.Add( new Object[4] { 66, "matt", "smith", "shipped to
office" } );
//(row for PersonID 67 was deleted)
//row changed:
dt.Rows.Add( new Object[4] { 68, "cory", "default", "shipped to
OFFICE" } );
//new row added:
dt.Rows.Add( new Object[4] { 69, "rich", "demel", "lives in hawaii"
} );
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds;
}
what am i doing wrong? would love to figure this technique out.
thanks!
matt
i have two datasets (only one DataTable each) representing the same set
of data, but from two different historic points of time. so they are of
the same structure, but there may have been rows added, removed, or
changed. each row has a unique ID column. i need a way to show my users
the row differences between the two. (much like using Visual Source
Safe's "Show Differences" on a local vs server versions of a file)
i read with glee that there is built-in support for getting the
differences between datasets. great! i read its done like so:
//get the two datasets data
DataSet historic = GetHistoricData();
DataSet current = GetCurrentData();
//make merge container
DataSet merged = new DataSet();
//seed w/ orig data
merged.Merge(historic);
merged.AcceptChanges();
//merge w/ new data
merged.Merge(current);
//and get the differences
DataSet added = merged.GetChanges(DataRowState.Added);
DataSet deleted = merged.GetChanges(DataRowState.Deleted);
DataSet modified = merged.GetChanges(DataRowState.Modified);
....however, that doesnt *quite* seem to work for me. in the above, only
dataset "added" has data, and the data in it is the exactly whats in
dataset "current".
here are the functions that make some test data:
public static DataSet GetHistoricData()
{
DataTable dt = new DataTable();
dt.Columns.Add("PersonID", System.Type.GetType("System.Int32"));
dt.Columns.Add("FirstName", System.Type.GetType("System.String"));
dt.Columns.Add("LastName", System.Type.GetType("System.String"));
dt.Columns.Add("Comments", System.Type.GetType("System.String"));
dt.Rows.Add( new Object[4] { 66, "matt", "smith", "shipped to
office" } );
dt.Rows.Add( new Object[4] { 67, "jonnie", "tyler", "shipped to
home" } );
dt.Rows.Add( new Object[4] { 68, "cory", "default", "shipped to
home" } );
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds;
}
//''''''''''''''''''''''''''''''''''''''''''''''''''''
public static DataSet GetCurrentData()
{
DataTable dt = new DataTable();
dt.Columns.Add("PersonID", System.Type.GetType("System.Int32"));
dt.Columns.Add("FirstName", System.Type.GetType("System.String"));
dt.Columns.Add("LastName", System.Type.GetType("System.String"));
dt.Columns.Add("Comments", System.Type.GetType("System.String"));
//row stayed the same:
dt.Rows.Add( new Object[4] { 66, "matt", "smith", "shipped to
office" } );
//(row for PersonID 67 was deleted)
//row changed:
dt.Rows.Add( new Object[4] { 68, "cory", "default", "shipped to
OFFICE" } );
//new row added:
dt.Rows.Add( new Object[4] { 69, "rich", "demel", "lives in hawaii"
} );
DataSet ds = new DataSet();
ds.Tables.Add(dt);
return ds;
}
what am i doing wrong? would love to figure this technique out.
thanks!
matt