save DataGridView to Xml

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henok Girma

    save DataGridView to Xml

    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
    DataGridViewTex tBoxCell, DataGridViewCom boBoxCell etc, i want to export all
    that to XML..

    Any help is greatly appreciated.



  • A.Kahtava

    #2
    Re: save DataGridView to Xml

    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.WriteXm l() method.

    The DataSet Class:


    Comment

    • Henok Girma

      #3
      Re: save DataGridView to Xml

      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" <kahtava@gmail. com> wrote in message
      news:1142209639 .688041.206710@ z34g2000cwc.goo glegroups.com.. .[color=blue]
      > 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.WriteXm l() method.
      >
      > The DataSet Class:
      > http://msdn.microsoft.com/library/de...ClassTopic.asp
      >[/color]


      Comment

      • Henok Girma

        #4
        Re: save DataGridView to Xml

        anyone?

        "Henok Girma" <nospam@spamfre e.com> wrote in message
        news:Qd5Rf.8179 25$x96.97819@at tbi_s72...[color=blue]
        > 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" <kahtava@gmail. com> wrote in message
        > news:1142209639 .688041.206710@ z34g2000cwc.goo glegroups.com.. .[color=green]
        >> 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.WriteXm l() method.
        >>
        >> The DataSet Class:
        >> http://msdn.microsoft.com/library/de...ClassTopic.asp
        >>[/color]
        >
        >[/color]


        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: save DataGridView to Xml

          Henok Girma wrote:[color=blue]
          > anyone?[/color]

          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

          Comment

          • Kevin Spencer

            #6
            Re: save DataGridView to Xml

            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" <nospam@spamfre e.com> wrote in message
            news:SWeRf.8196 02$x96.730159@a ttbi_s72...[color=blue]
            > anyone?
            >
            > "Henok Girma" <nospam@spamfre e.com> wrote in message
            > news:Qd5Rf.8179 25$x96.97819@at tbi_s72...[color=green]
            >> 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" <kahtava@gmail. com> wrote in message
            >> news:1142209639 .688041.206710@ z34g2000cwc.goo glegroups.com.. .[color=darkred]
            >>> 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.WriteXm l() method.
            >>>
            >>> The DataSet Class:
            >>> http://msdn.microsoft.com/library/de...ClassTopic.asp
            >>>[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • Henok Girma

              #7
              Re: save DataGridView to Xml

              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" <kevin@DIESPAMM ERSDIEtakempis. com> wrote in message
              news:%23nZqXArR GHA.2176@TK2MSF TNGP10.phx.gbl. ..[color=blue]
              >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" <nospam@spamfre e.com> wrote in message
              > news:SWeRf.8196 02$x96.730159@a ttbi_s72...[color=green]
              >> anyone?
              >>
              >> "Henok Girma" <nospam@spamfre e.com> wrote in message
              >> news:Qd5Rf.8179 25$x96.97819@at tbi_s72...[color=darkred]
              >>> 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" <kahtava@gmail. com> wrote in message
              >>> news:1142209639 .688041.206710@ z34g2000cwc.goo glegroups.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.WriteXm l() method.
              >>>>
              >>>> The DataSet Class:
              >>>> http://msdn.microsoft.com/library/de...ClassTopic.asp
              >>>>
              >>>
              >>>[/color]
              >>
              >>[/color]
              >
              >[/color]


              Comment

              • M. Matsuda

                #8
                Re: save DataGridView to Xml

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

                "Henok Girma" wrote:
                [color=blue]
                > 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" <kevin@DIESPAMM ERSDIEtakempis. com> wrote in message
                > news:%23nZqXArR GHA.2176@TK2MSF TNGP10.phx.gbl. ..[color=green]
                > >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" <nospam@spamfre e.com> wrote in message
                > > news:SWeRf.8196 02$x96.730159@a ttbi_s72...[color=darkred]
                > >> anyone?
                > >>
                > >> "Henok Girma" <nospam@spamfre e.com> wrote in message
                > >> news:Qd5Rf.8179 25$x96.97819@at tbi_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" <kahtava@gmail. com> wrote in message
                > >>> news:1142209639 .688041.206710@ z34g2000cwc.goo glegroups.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.WriteXm l() method.
                > >>>>
                > >>>> The DataSet Class:
                > >>>> http://msdn.microsoft.com/library/de...ClassTopic.asp
                > >>>>
                > >>>
                > >>>
                > >>
                > >>[/color]
                > >
                > >[/color]
                >
                >
                >[/color]

                Comment

                • Jonte

                  #9
                  Re: save DataGridView to Xml

                  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.1 2</ip>
                  <ip>192.168.1.1 3</ip>
                  <ip>192.168.1.1 4</ip>
                  <ip>192.168.1.1 5</ip>
                  <ip>192.168.1.1 6</ip>
                  <ip>192.168.1.1 7</ip>
                  </iplista>

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

                  public Form1()
                  {
                  InitializeCompo nent();
                  ipDataset.ReadX ml(filePath);
                  dataGridView1.D ataSource = ipDataset;
                  dataGridView1.D ataMember = "ip";
                  }

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

                  XmlNode currNode;
                  XmlDocumentFrag ment docFrag = doc.CreateDocum entFragment();
                  if (textBox1.Text != "")
                  {
                  docFrag.InnerXm l = "<ip>" + textBox1.Text + "</ip>";
                  // insert the "data" node at the end of the xml file.
                  currNode = doc.DocumentEle ment;
                  currNode.Insert After(docFrag, currNode.LastCh ild);
                  //save the output to a file
                  doc.Save(filePa th);
                  //Update the DATAGRID_VIEW!! !!!??????
                  // LoadXml(filePat h);
                  }
                  else
                  {
                  MessageBox.Show ("Fyll i nått värde i textbox1");
                  }
                  }


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


                  "M. Matsuda" wrote:
                  [color=blue]
                  > I might have some sample to help. If you don't mind can you tell me where to
                  > send sample code?
                  >
                  > "Henok Girma" wrote:
                  >[color=green]
                  > > 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" <kevin@DIESPAMM ERSDIEtakempis. com> wrote in message
                  > > news:%23nZqXArR GHA.2176@TK2MSF TNGP10.phx.gbl. ..[color=darkred]
                  > > >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" <nospam@spamfre e.com> wrote in message
                  > > > news:SWeRf.8196 02$x96.730159@a ttbi_s72...
                  > > >> anyone?
                  > > >>
                  > > >> "Henok Girma" <nospam@spamfre e.com> wrote in message
                  > > >> news:Qd5Rf.8179 25$x96.97819@at tbi_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" <kahtava@gmail. com> wrote in message
                  > > >>> news:1142209639 .688041.206710@ z34g2000cwc.goo glegroups.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.WriteXm l() method.
                  > > >>>>
                  > > >>>> The DataSet Class:
                  > > >>>> http://msdn.microsoft.com/library/de...ClassTopic.asp
                  > > >>>>
                  > > >>>
                  > > >>>
                  > > >>
                  > > >>
                  > > >
                  > > >[/color]
                  > >
                  > >
                  > >[/color][/color]

                  Comment

                  • Nilotic NordlePips

                    #10
                    Re: save DataGridView to Xml


                    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.W riteXmlData(fil epath/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 ***

                    Comment

                    • Nick

                      #11
                      Re: save DataGridView to Xml



                      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 ***

                      Comment

                      Working...