• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 773
  • Last Modified:

How do you get the Deleted DataRows of a DataSet?

My program uses DataRow.Delete to delete rows from a DataSet.  After they have been deleted, I check it's RowState which is Deleted and then I call GetChanges() on the DataSet.  The changes returned do not include the deleted rows.  If I can't use GetChanges after a series of Delete()'s to find out what has changed in the DataSet, what can I use to find out wihch rows  have been deleted.  Do I need to keep track of the deletions myself?

My code to delete and get changes is below.


int rowIdx = dataGrid1.CurrentRowIndex;
DataSet ds = (DataSet)dataGrid1.DataSource;
DataRow rowToDelete = ds.Tables["Msgs"].Rows[rowIdx];
rowToDelete.Delete();

DataSet changes = ds.GetChanges();
if (changes != null)
{
     string changesString = changes.GetXml();
     MessageBox.Show(changesString);
}

0
mromeo
Asked:
mromeo
  • 4
  • 3
2 Solutions
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
You can use ds.GetChanges(DataRowState.Deleted) to get only the deleted rows.
0
 
mromeoAuthor Commented:
What am I supposed to get.  When I do

      DataSet changes = ds.GetChanges(DataRowState.Deleted);
      string c = changes.GetXml();

The string returned is just:

<MessageList xmlns=\"http://tempuri.org/Messages.xsd\" />

How can I get changes from that?


0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
That's strange, are you sure you've deleted the rows? And not accidently called AcceptChanges or RejectChanges on the dataset?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
mromeoAuthor Commented:


What should GetXml return?  Should it have the entire row in the xml string?
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
It should contain the whole dataset, including table and column definitions and all data. It looks like the dataset in question is completely empty.
0
 
sumixCommented:
Your code is ok, 'changes' dataset contains the deleted row (you can simply check changes.Tables["Msgs"].Rows.Count value). GetXml function is the one that doesn't get the deleted rows. If you want to put all your modified rows in a xml file you may consider using WriteXml function specifing the XmlWriteMode.DiffGram as second parameter
 (like changes.WriteXml("f1.xml",XmlWriteMode.DiffGram))  
0
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
What you could also so is to call changes.RejectChanges(); and then call GetXml. This will not harm the original dataset as the rows are cloned to the new set.
0
 
mromeoAuthor Commented:
RejectChanges works great!  The DiffGram would also work, but it's more work than is needed.  I'll split up the points.  Thanks
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now