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,
Andreas
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
- // delete the selected row
- dgvMain.Rows.Remove(dgvMain.CurrentRow);
Expand|Select|Wrap|Line Numbers
- // init the data set
- this.books = new DataSet("Books DataSet");
- // read data from xml document
- this.books.ReadXml("Books.xml");
- // give some information of the data to the DataGrid
- dgvMain.DataSource = this.books.DefaultViewManager;
- dgvMain.DataMember = "Book";
Expand|Select|Wrap|Line Numbers
- // get the changed data
- this.books = (DataSet)dgvMain.DataSource;
- // write changed data to xml file
- this.books.WriteXml("Books.xml", XmlWriteMode.IgnoreSchema);
--> Was possible using the DataGrid, but I don't want to use the DataGrid anymore