Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a datgridview whose values are filled at run-time when the user decides to add a row.The same values are also written to a XML file after it is added to the DataGridView.
But instead of adding data to the file, the previously added data also gets deleted from the XML file and the form also freezes without adding row in the datagridview.

My code for creating the xml file is :
C#
private void createXML()
        {
            DataTable dt = new DataTable();
            foreach (DataGridViewColumn col in dataGridView1.Columns)
            {
                dt.Columns.Add(col.HeaderText);
            }
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                DataRow dRow = dt.NewRow();
                foreach (DataGridViewCell cell in row.Cells)
                {
                    dRow[cell.ColumnIndex] = cell.Value;
                }
                dt.Rows.Add(dRow);
            }
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            ds.WriteXml(@"D:/HostAdded.xml");
        }


Please help!!!
its urgent :)
Posted
Comments
Naz_Firdouse 28-May-14 7:13am    
what is the error you are getting?
Naz_Firdouse 28-May-14 7:14am    
are you sure that dt contains all the rows of the gridview?
sovit agarwal 28-May-14 7:17am    
Sometimes i am getting the error as "Child list for field Table cannot be created" and sometimes the previous values of the file and datagridview are both getting deleted and at times its working fine...
And i dont have clear idea if my dt contains all rows of the gridview
Naz_Firdouse 28-May-14 8:15am    
repost
Getting error while adding a new row to a XML file from a datgridview?[^]
Use "Imporve question" option istead of reposting the question with some changes

1 solution

The absolutely simplest way to do this is to delete the existing XML file and recreate it. Otherwise you could be facing an XPath learning curve. FYI, I use this method myself.

Try Googling "DataGridView to Xml" for other ways.
 
Share this answer
 
v3

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