2 Replies - 10681 Views - Last Post: 14 April 2009 - 12:39 AM Rate Topic: -----

#1 Servatis  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 394
  • Joined: 30-March 09

writing DataGridView to an xml file

Posted 10 April 2009 - 03:33 AM

hello,

I have a small windows application which reads from an xml file and then displays it into a DataGridView.

private void Form1_Load(object sender, EventArgs e)
{
	XmlDataDocument xmlDatadoc = new XmlDataDocument();

	xmlDatadoc.DataSet.ReadXml("C:\\testxml.xml");

	DataSet ds = new DataSet("Books DataSet");

	ds = xmlDatadoc.DataSet;

	dataGridView1.DataSource = ds.DefaultViewManager;

	dataGridView1.DataMember = "Book";
}


this works fine and it displays the data perfectly, but now i wan't to be able to edit the data in the DataGridView and press a button to write the edited data to the xml file.

I have tried putting the DataGridView back into the created DataSet but i have yet to find a way to do this.

Is there a way to put the DataGridView into a DataSet? maybe looping through the DataGridView? i have no idea where to start with this.

or would it maybe be easier to use an .mdb for this, with OleDb?
my main goal is to be able to read data into a DataGridView and then be able to edit the data and save it.

Is This A Good Question/Topic? 0
  • +

Replies To: writing DataGridView to an xml file

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: writing DataGridView to an xml file

Posted 10 April 2009 - 11:10 AM

Cast the DataSource property of the DataGridView back to a DataSet.

Example:
DataSet myData = (DataSet)dataGridView1.DataSource;



If you are storing or update a lot of records, then I would definitely suggest you use a database. However, if you only have a few records then storing them in an XML file will work just fine.
Was This Post Helpful? 1
  • +
  • -

#3 Servatis  Icon User is offline

  • D.I.C Regular

Reputation: 48
  • View blog
  • Posts: 394
  • Joined: 30-March 09

Re: writing DataGridView to an xml file

Posted 14 April 2009 - 12:39 AM

thank you for your reply.

I have tried the code in your post but this gives me an error.

Cannot convert object type System.Data.DataViewManager to System.Data.DataSet.

this is my code as it is now:
private void Form1_Load(object sender, EventArgs e)
{
	XmlDataDocument xmlDatadoc = new XmlDataDocument();

	xmlDatadoc.DataSet.ReadXml("C:\\testxml.xml");

	ds = new DataSet();

	ds = xmlDatadoc.DataSet;

	dataGridView1.DataSource = ds.DefaultViewManager;

	dataGridView1.DataMember = "Book";
}

private void button1_Click(object sender, EventArgs e)
{
	ds = (DataSet)dataGridView1.DataSource;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1