save DataGridView to Xml

H

Henok Girma

Hello Gurus,
I want to save the state of an unbound DataGridView on my Windows Form
application to an XML file so i can later load it back..

Basically, on my form I have a DataGridView, it's got some
DataGridViewTextBoxCell, DataGridViewComboBoxCell etc, i want to export all
that to XML..

Any help is greatly appreciated.
 
H

Henok Girma

What i wanted to store was the value of those items, while i can iterate
create line by line the xml file, i would much rather use the dataset and
WriteXml method, but for some reason i was not able to do so for an unbound
DataGridView. the user creates the rows and sets the values, then when they
click save, i want to output all the data to an XML file, idealy, i want to
extract the DataSet of the DataGridView to save it to XML using the WriteXML
method mentioned, but i was not able to get it.

Do i make sense?
 
H

Henok Girma

anyone?

Henok Girma said:
What i wanted to store was the value of those items, while i can iterate
create line by line the xml file, i would much rather use the dataset and
WriteXml method, but for some reason i was not able to do so for an
unbound DataGridView. the user creates the rows and sets the values, then
when they click save, i want to output all the data to an XML file,
idealy, i want to extract the DataSet of the DataGridView to save it to
XML using the WriteXML method mentioned, but i was not able to get it.

Do i make sense?
 
K

Kevin Spencer

A DataGridView is a user interface component, not a data component. It is
bound to a DataSet by means of a BindingSource, which relates the data in a
particular table or view in the DataSet to the DataGridView. The
DataGridView then uses the BindingSource to display the data in a meaningful
way. Therefore, you don't want to serialize a DataGridView at all; you want
to serialize the data that is contained (or rather "displayed") in it. This
would entail serializing the DataSet.

As to why you were "not able to get it," that is the crux of the problem,
and cannot be answered until you describe exactly what you tried, and what
the results were.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer

Presuming that God is "only an idea" -
Ideas exist.
Therefore, God exists.
 
H

Henok Girma

ok, now we are getting somewhere.. i understand about the DataSet and the
bindingsource etc, but since i don't use a dataset to populate my
DataGridView, i cannot extract the data to xml with the WriteXML method.. so
what i was looking for was a way to extract a DataSet out of the
DataGridView eventough it was not bounded, i know this doesn't makse sense..
let me explain a bit..

I create rows programatically, say after 10 rows have been created, i want
to save that information to a file so that next time my application runs,
the datagridview is populated again.. i think from the looks of it, there is
no easy way to do it than to iterate and write each row information to an
xml file and read it back to re-create the rows when the application starts
up..

i was thinking there was an easy method to export/serialize the data out a
datagridview..

any suggestions?
 
G

Guest

I might have some sample to help. If you don't mind can you tell me where to
send sample code?
 
G

Guest

Hello! Perhaps this will help you, some code to look into:
IP.xml :
<?xml version="1.0" encoding="utf-8" ?>
<iplista>
<ip>192.168.1.12</ip>
<ip>192.168.1.13</ip>
<ip>192.168.1.14</ip>
<ip>192.168.1.15</ip>
<ip>192.168.1.16</ip>
<ip>192.168.1.17</ip>
</iplista>

Display data in dataGridview and save it:
//
string filePath = "IP.xml";

public Form1()
{
InitializeComponent();
ipDataset.ReadXml(filePath);
dataGridView1.DataSource = ipDataset;
dataGridView1.DataMember = "ip";
}

// Update button in winForm and a textbox1 to write data to save in xml
file...
private void button2_Click(object sender, EventArgs e)
{
XmlTextReader reader = new XmlTextReader(filePath);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
reader.Close();

XmlNode currNode;
XmlDocumentFragment docFrag = doc.CreateDocumentFragment();
if (textBox1.Text != "")
{
docFrag.InnerXml = "<ip>" + textBox1.Text + "</ip>";
// insert the "data" node at the end of the xml file.
currNode = doc.DocumentElement;
currNode.InsertAfter(docFrag, currNode.LastChild);
//save the output to a file
doc.Save(filePath);
//Update the DATAGRID_VIEW!!!!!??????
// LoadXml(filePath);
}
else
{
MessageBox.Show("Fyll i nått värde i textbox1");
}
}


******************HOPE IT HELPS YOU ON THE WAY*************
 
N

Nilotic NordlePips

Hi , I understand what you're trying to do, and I agree it seems
strangely clunky to move data back from the edited DataGridView. Given
the excellence elsewhere it seems odd that we can't just say:

dataGridView1.WriteXmlData(filepath/StreamWriter object)

or .WriteUpdate

I wish I could be of more help ; perhaps this will spark a response
that does help more though :)

N
 
N

Nick

I'm looking to do the same thing. Using the datagridview as an input
control and want to save to XML. Ever figure a way out or do you need
to bind a dataset?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top