• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 781
  • 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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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