473,385 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Using XML-Files with the DataGridView

1
Hello!

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
  1.       // delete the selected row
  2.       dgvMain.Rows.Remove(dgvMain.CurrentRow);
  3.  
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");
  3.  
  4.       // read data from xml document
  5.       this.books.ReadXml("Books.xml");
  6.  
  7.       // give some information of the data to the DataGrid
  8.       dgvMain.DataSource = this.books.DefaultViewManager;
  9.       dgvMain.DataMember = "Book";
  10.  
Code for saving the changes
Expand|Select|Wrap|Line Numbers
  1.       // get the changed data
  2.       this.books = (DataSet)dgvMain.DataSource;
  3.  
  4.       // write changed data to xml file
  5.       this.books.WriteXml("Books.xml", XmlWriteMode.IgnoreSchema);
  6.  
--> Throws an InvalidCastException
--> Was possible using the DataGrid, but I don't want to use the DataGrid anymore
Oct 2 '09 #1
1 5324
Plater
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

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

Similar topics

0
by: anonieko | last post by:
> > > > Writing an XML document the .Net way If you've been using the .Net Framework for even a week, you know that the kids in Redmond really thought of almost everything, so they're not...
3
by: vanisathish | last post by:
Hi I am running a client side javascript timer to periodically refresh the contents of some tables in the HTML page. The table values are dynmically binded from XML DOM object using the <XML tag...
5
by: Stuart Robertson | last post by:
I am trying to find a solution that will allow me to use XmlSerializer to serialize/deserialize a collection of objects where a given object is shared between two or more other objects, and not...
8
by: FS Liu | last post by:
Hi, I am writing ATL Service application (XML Web service) in VS.NET C++. Are there any sample programs that accept XML as input and XML as output in the web service? Thank you very much.
5
by: David Lozzi | last post by:
Howdy, I wrote a web service in .Net for my customer. My customer has another vendor who now has to consume it but they are not using Visual Studio. Most of their pages are jsp, and they said...
1
by: Daniel | last post by:
Does system.xml have any way to transofrm data with an xswl style sheet using strings like MSXML2 does? how to convert this to use System.XML so i do not depend on MSXML2 interop? static...
6
by: Derek Hart | last post by:
I bring in an xml file into vb.net by using xmlDoc.LoadXml(XMLString) - I run xpath statements against the xml file to grab data from it, so I use, as an example, //Vehicles/Vehicles/@make to get...
0
by: =?Utf-8?B?Y2luZHk=?= | last post by:
I know I wrote before a week ago when I knew even less than now but I am getting better please anyone give me a clue or an example. Am I completely off track? I have a datarow in a table with the...
2
by: rrflore2 | last post by:
Ok. I'm writing and deleting to an xml file using a dataset. I have a function in my codebehind page that binds a listbox to the dataset that performs the writes/deletes. Everything seems to be...
0
by: panos100m | last post by:
Hi i can read everything from my xml file except a part that i can always get teh first items of each category (it will make more sence in a sec) So here is my xml <?xml version="1.0"...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.