Hi All !
I want to save or Export the content(Row And Column Cell) of a DatGridView To a XML file .how can i do it ? there is No DataSet Or DataTable for DataGridView .
thanks in advance for any Help !

6 Years
Discussion Span
Last Post by s_mostafa_h

There are a few threads that are about exporting to Xml using c#, take a look at those and try and apply them to suite your needs (its the same principle, put the data from the datagGrid into variables and export them in a structured tree format). if you run into problems just post the code and issues back here.


Thanks for ur reply , Here My Code :

SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.Filter = "Xml files (*.xml)|*.xml";
            saveDialog.FilterIndex = 2;
            saveDialog.RestoreDirectory = true;
            saveDialog.InitialDirectory = "d:\\";
            saveDialog.FileName = "XML File";
            saveDialog.Title = "XML Export";

            if (saveDialog.ShowDialog() == DialogResult.OK)
                string OutPutXMLFile = "";
                System.Xml.XmlDocument XMLDoc = new System.Xml.XmlDocument();
                for (int i = 0; i < DataGridView1.Rows.Count; i++)
                    OutPutXMLFile = "";
                    for (int j = 0; j < DataGridView1.Columns.Count; j++)
                        OutPutXMLFile += String.Format("{0:0.00}", DataGridView1.Rows[i].Cells[j].Value);


But I Have some Error to create a xml file !
regards !


Perhaps consider something more like this:

string GridToXml(DataGridView source, string root, string element)
    var doc = new XmlDocument();

    doc.LoadXml("<" + root + " />");

    foreach (var row in source.Rows)
        var element = doc.CreateElement(element);

        foreach (var cell in row.Cells)
            element.LastChild.InnerText = cell.FormattedValue;


    return doc.OuterXml;

I use C#.net 2005 and there is no keyword for "var" . apparently it added in 2010 (later) . is it equal "Type"?


Replace var with whatever type name you're assigning to the variable. var row in source.Rows would become DataGridViewRow row in source.Rows . You'd know that if you actually read some of the links from that search instead of just determining that var wasn't supported on your older compiler.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.