Click here to Skip to main content
15,879,239 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A datagridview is populated with data using a datatable (dt).
The user can add, delete or update a row in the grid...
The dt can see which rows have been modified, added or deleted...

Question:
1-
What is the best way to place this new dt into an xml string?
i.e. Should there be three different xml strings? one for added rows, one for deleted rows and one for modified rows? or should there be just one xml string which has all three different rowstate rows in it?
p.s. I

This is what I am trying to do but not sure the best way to build the xml to identify which row has what state.

C#
int intCount = 0;
            foreach (DataRow dr in dt.Rows)
            {
                //get column positions...
                //int intCustIDColumn = dgv.Columns["CustID"].Index;
                //int intValueColumn = dgv.Columns["ValueDate"].Index;

                if (dr.RowState == DataRowState.Added)
                {
                    //added record...

                    //int intCustID = int.Parse(dt.Rows[intCount][intCustColumn].ToString());
                    //DateTime dtValue = DateTime.Parse(dt.Rows[intCount][intValueColumn].ToString());

                }
                else if (dr.RowState == DataRowState.Modified)
                {
                    //existing row modified...
                }
                else if (dr.RowState == DataRowState.Deleted)
                {
                    //row deleted...
                }

                intCount++;
            }

And this is how I will be building an xml string:
////Getting the datatable into a streamwriter...
            //string strXML;
            //using (StringWriter sw = new StringWriter())
            //{
		//use the added datatable to build a xml string for the added rows only
            //    dtAdded.WriteXml(sw)
            //}


Thanks
Posted

1 solution

I think a little clarification is required.
Are you:
1. trying to produce 3 XML files? One file for added rows, one for deleted and one for modified?
OR
2. do you want 1 XML file with the complete table but containing an attribute on each row to indicate the added/modified/deleted status?

If it's
1. create 3 new DataTables. One each for rows Added/Delete and Modified. Add rows to each table within the conditions in your 1st loop. When you've built the tables call:
C#
dtAdded.WriteXml("MyAddedRows.xml");
dtModified.WriteXml("MyModRows.xml");
dtDeleted.WriteXml("MyDeletedRows.xml");

OR
2. You can't use the DataTable.WriteXml() method because the row status is not generated. You'll have to extract all the data from the table and build an XML file yourself using XmlWriter.
 
Share this answer
 
Comments
arkiboys 22-Sep-11 12:42pm    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900