By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,070 Members | 1,718 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,070 IT Pros & Developers. It's quick & easy.

save DataGridView to Xml

P: n/a
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.

Mar 13 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Do you really want to save a TextBox, ComboBox and data to an XML file?

Generally DataGrids are displaying data from a DataSet, you can save
the data within a DataSet through the DataSet.WriteXml() method.

The DataSet Class:
http://msdn.microsoft.com/library/de...ClassTopic.asp

Mar 13 '06 #2

P: n/a
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?

"A.Kahtava" <ka*****@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Do you really want to save a TextBox, ComboBox and data to an XML file?

Generally DataGrids are displaying data from a DataSet, you can save
the data within a DataSet through the DataSet.WriteXml() method.

The DataSet Class:
http://msdn.microsoft.com/library/de...ClassTopic.asp

Mar 13 '06 #3

P: n/a
anyone?

"Henok Girma" <no****@spamfree.com> wrote in message
news:Qd5Rf.817925$x96.97819@attbi_s72...
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?

"A.Kahtava" <ka*****@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Do you really want to save a TextBox, ComboBox and data to an XML file?

Generally DataGrids are displaying data from a DataSet, you can save
the data within a DataSet through the DataSet.WriteXml() method.

The DataSet Class:
http://msdn.microsoft.com/library/de...ClassTopic.asp


Mar 13 '06 #4

P: n/a
Henok Girma wrote:
anyone?


It would help if you gave a bit more information. "I was not able to
get it" doesn't give us much to go on.

Could you post a short but complete program that demonstrates the
problem? See
http://www.pobox.com/~skeet/csharp/complete.html for what I mean by
that.

Jon

Mar 13 '06 #5

P: n/a
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.

"Henok Girma" <no****@spamfree.com> wrote in message
news:SWeRf.819602$x96.730159@attbi_s72...
anyone?

"Henok Girma" <no****@spamfree.com> wrote in message
news:Qd5Rf.817925$x96.97819@attbi_s72...
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?

"A.Kahtava" <ka*****@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Do you really want to save a TextBox, ComboBox and data to an XML file?

Generally DataGrids are displaying data from a DataSet, you can save
the data within a DataSet through the DataSet.WriteXml() method.

The DataSet Class:
http://msdn.microsoft.com/library/de...ClassTopic.asp



Mar 13 '06 #6

P: n/a
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?

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

"Henok Girma" <no****@spamfree.com> wrote in message
news:SWeRf.819602$x96.730159@attbi_s72...
anyone?

"Henok Girma" <no****@spamfree.com> wrote in message
news:Qd5Rf.817925$x96.97819@attbi_s72...
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?

"A.Kahtava" <ka*****@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
Do you really want to save a TextBox, ComboBox and data to an XML file?

Generally DataGrids are displaying data from a DataSet, you can save
the data within a DataSet through the DataSet.WriteXml() method.

The DataSet Class:
http://msdn.microsoft.com/library/de...ClassTopic.asp



Mar 13 '06 #7

P: n/a
I might have some sample to help. If you don't mind can you tell me where to
send sample code?

"Henok Girma" wrote:
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?

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

"Henok Girma" <no****@spamfree.com> wrote in message
news:SWeRf.819602$x96.730159@attbi_s72...
anyone?

"Henok Girma" <no****@spamfree.com> wrote in message
news:Qd5Rf.817925$x96.97819@attbi_s72...
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?

"A.Kahtava" <ka*****@gmail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com...
> Do you really want to save a TextBox, ComboBox and data to an XML file?
>
> Generally DataGrids are displaying data from a DataSet, you can save
> the data within a DataSet through the DataSet.WriteXml() method.
>
> The DataSet Class:
> http://msdn.microsoft.com/library/de...ClassTopic.asp
>



Mar 24 '06 #8

P: n/a
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*************
"M. Matsuda" wrote:
I might have some sample to help. If you don't mind can you tell me where to
send sample code?

"Henok Girma" wrote:
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?

"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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.

"Henok Girma" <no****@spamfree.com> wrote in message
news:SWeRf.819602$x96.730159@attbi_s72...
> anyone?
>
> "Henok Girma" <no****@spamfree.com> wrote in message
> news:Qd5Rf.817925$x96.97819@attbi_s72...
>> 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?
>>
>> "A.Kahtava" <ka*****@gmail.com> wrote in message
>> news:11**********************@z34g2000cwc.googlegr oups.com...
>>> Do you really want to save a TextBox, ComboBox and data to an XML file?
>>>
>>> Generally DataGrids are displaying data from a DataSet, you can save
>>> the data within a DataSet through the DataSet.WriteXml() method.
>>>
>>> The DataSet Class:
>>> http://msdn.microsoft.com/library/de...ClassTopic.asp
>>>
>>
>>
>
>


Jun 8 '06 #9

P: n/a

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


*** Sent via Developersdex http://www.developersdex.com ***
Jun 16 '06 #10

P: n/a


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?

*** Sent via Developersdex http://www.developersdex.com ***
Sep 19 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.