I am a beginner in c#, I created one dataGridView1 in a Form to which I added some rows and columns (without using DataSet and Datatable). Now I need to send the data of dataGridView1 to an xml file. The xml file should update whenever I click on that button. I need to do this without using Datatable (or tables). I tried the below code (not working)
OnButtonClick
XmlTextWriter newXml = new XmlTextWriter("d:/newXML.xml", Encoding.UTF8);
DataSet ds = new DataSet(dataGridView1.Rows.ToString()); /* May be I am missing something here */
ds.WriteXml(newXml);
xml file is created successfully but showing <System.Windows.Forms.DataGridViewRowCollection /> in it.
If I add BindingSource bs = (BindingSource)dataGridView1.DataSource; before DataSet, it is showing error “Object reference not set to an instance of an object.”
Note: If the xml file is not their then it should create one.
Thanks in advance.
You can instantiate a
DataTableand populate it from yourdataGridView1control:then continue with the rest of your code: