469,957 Members | 2,710 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,957 developers. It's quick & easy.

Using XML-Files with the DataGridView


I have a form application with a DataGridView, and an Save/Delete and Cancel button. When the application is opened, it is supplied an XML file that may or may not exist, and display the contents. Regardless of whether or not the file exists, the user must be able to modify the data in each cell in the DataGridView, along with being able to add or remove rows. When the Save button is clicked, the contents of the DataGridView must be exported to the XML file that was initially supplied.

I am able to delete a row pressing the "Entf" key, but not by klicking the Delete button in my application and I am also not able to save the changes done whitin the DataGridView.

I tried to solve this problem for several hours now, and I can't seem to get all of these requirements working at once. There are many tutorials and code snippeds on how to use the DataGridView with xml files, but most of them only describe how to load an xml file into the DataGridVew object.

Could someone help me to solve my problems?

Thank you.

Best regards,


PS: There is an error at your user activation side.
Thanks for your registration $username.
Code to delete a row
Expand|Select|Wrap|Line Numbers
  1.       // delete the selected row
  2.       dgvMain.Rows.Remove(dgvMain.CurrentRow);
Code for loading the xml file into the DataGridView object
Expand|Select|Wrap|Line Numbers
  1.       // init the data set
  2.       this.books = new DataSet("Books DataSet");
  4.       // read data from xml document
  5.       this.books.ReadXml("Books.xml");
  7.       // give some information of the data to the DataGrid
  8.       dgvMain.DataSource = this.books.DefaultViewManager;
  9.       dgvMain.DataMember = "Book";
Code for saving the changes
Expand|Select|Wrap|Line Numbers
  1.       // get the changed data
  2.       this.books = (DataSet)dgvMain.DataSource;
  4.       // write changed data to xml file
  5.       this.books.WriteXml("Books.xml", XmlWriteMode.IgnoreSchema);
--> Throws an InvalidCastException
--> Was possible using the DataGrid, but I don't want to use the DataGrid anymore
Oct 2 '09 #1
1 4972
7,872 Expert 4TB
dgvMain.DataSource is not going to be a dataset. It's going to be a view of some sort, cast it as the correct object, then use the property of that view object to get the dataset.
Also, unless you have multiple tables in your dataset, its worth it to just use a DataTable object instead of the DataSet object.
Oct 5 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by anonieko | last post: by
5 posts views Thread by David Lozzi | last post: by
6 posts views Thread by Derek Hart | last post: by
reply views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.