M
Matt Burland
I have a dataset which consists of a main table and a bunch of child tables.
I want, firstly, to be able to detect if a particular record has changed,
but is there an easy and quick way to tell if the children of a datarow in
the parent table have been updated without iterating through all the
relationships and checking each one?
Currently I have this:
public bool HasChanges
{
get
{
DataRowView drv = (DataRowView)database[index.Position];
bool changed = (drv.Row.RowState == DataRowState.Modified);
foreach (DataRelation rel in database.Data.Relations)
{
if (changed)
break;
if (rel.ParentTable == database.Data.Tables["tblMain"])
{
DataRow[] child = drv.Row.GetChildRows(rel);
foreach(DataRow r in child)
{
changed = changed | r.RowState == DataRowState.Modified;
if (changed)
break;
}
}
}
return changed;
}
}
Which even with the breaks, which will let the program get out early once
it's found one change, still seems like it's horribly inefficent, especially
since most of the time there really won't be any changes. I want to be able
to display on my form (which shows one record from the main table plus info
from the child table) if this record has changed since being loaded from the
database, so I need to do this everytime the user navigates to a new record
in the database. Any ideas?
My other, related, question is how can I count how many records have been
changed? I can use GetChanges on the parent table and to get changes there
and then use GetChanges on all the child tables too. But if I add them
together I'm likely to overcount some records (where both the parent and the
child have been changed). Is there an efficient way to deal with this? This
is less of an issue than the first case since I don't expect to need to get
that number very often.
Any insight here is most welcome!
Thanks
I want, firstly, to be able to detect if a particular record has changed,
but is there an easy and quick way to tell if the children of a datarow in
the parent table have been updated without iterating through all the
relationships and checking each one?
Currently I have this:
public bool HasChanges
{
get
{
DataRowView drv = (DataRowView)database[index.Position];
bool changed = (drv.Row.RowState == DataRowState.Modified);
foreach (DataRelation rel in database.Data.Relations)
{
if (changed)
break;
if (rel.ParentTable == database.Data.Tables["tblMain"])
{
DataRow[] child = drv.Row.GetChildRows(rel);
foreach(DataRow r in child)
{
changed = changed | r.RowState == DataRowState.Modified;
if (changed)
break;
}
}
}
return changed;
}
}
Which even with the breaks, which will let the program get out early once
it's found one change, still seems like it's horribly inefficent, especially
since most of the time there really won't be any changes. I want to be able
to display on my form (which shows one record from the main table plus info
from the child table) if this record has changed since being loaded from the
database, so I need to do this everytime the user navigates to a new record
in the database. Any ideas?
My other, related, question is how can I count how many records have been
changed? I can use GetChanges on the parent table and to get changes there
and then use GetChanges on all the child tables too. But if I add them
together I'm likely to overcount some records (where both the parent and the
child have been changed). Is there an efficient way to deal with this? This
is less of an issue than the first case since I don't expect to need to get
that number very often.
Any insight here is most welcome!
Thanks