locked
Saving DataGridView data to xml RRS feed

  • Question

  • Hi,

    I need first time help to save a DataGridView to xml. 

    Below is what I've been testing.  I need help where you see "????" and I don't have decent code to show here as I really don't know what to do.  I also included an example of what is being populated in the DataGridView from the xml file/schema from the start (see very bottom), which of course I'd like to be able to add more data to it via the DataGridView and then save it back to xml.

    Thanks for your help.

    --------------------------------------

     

     

    Private Sub Button2_Click_1(sender As System.Object, e As System.EventArgs) Handles Button2.Click

     

            Dim dt As New DataTable("testtable")

     

            dt.Columns.Add("Department", GetType(String))

            dt.Columns.Add("Contact", GetType(String))

            dt.Columns.Add("Role", GetType(String))

            dt.Columns.Add("Expertise", GetType(String))

     

           ????

     

            dt.AcceptChanges()

            Me.DataGridView1.DataSource = dt.DefaultView

     

            Dim dtx As DataTable = DirectCast(Me.DataGridView1.DataSource, DataView).Table

            dtx.WriteXml(My.Settings.charterFiles & "\" & "Test.xml")

        End Sub

     

    --------------------

    <?xml version="1.0" encoding="utf-8"?>

    <departments> 

            <Department name="Corporate Compliance"> 

                <Contact>Mary Dewan</Contact> 

                <Role>Stakeholder Representitive</Role> 

                <Expertise>Business Analysis</Expertise> 

            </Department>

            <Department name="Corporate Compliance"> 

                <Contact>Compliance Team</Contact> 

                <Role>Project Customer</Role> 

                <Expertise>Business Process Auditing</Expertise> 

            </Department>

        </departments> 

     

     

    Wednesday, November 9, 2011 3:04 PM

Answers

  • Hi 8Terrapin4,

    I'm not sure what you are looking for, but it seems that you have most the basics presented and maybe just need some help with structuring your code to allow for:

        1. Create a blank Datagridview (you already have this code).
        2. Save entered data. (you already have the basic part of this.)
        3. Allow the user to load existing data and continuing editing it.

    So here is an example with three (3) buttons and a Datagridview on a form.  Button1 creates the blank entry Datagridview.  Button2 saves the data to an xml file.  Button3 loads an existing xml file.

    I leave it to you to handle the file name selection dialog and error handling.

       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          'create Table
          Dim dt As New DataTable("testtable")
          dt.Columns.Add("Department", GetType(String))
          dt.Columns.Add("Contact", GetType(String))
          dt.Columns.Add("Role", GetType(String))
          dt.Columns.Add("Expertise", GetType(String))

          With DataGridView1
             .DataSource = dt
             .AllowUserToAddRows = True : .AllowUserToDeleteRows = True
             .AllowUserToOrderColumns = False : .AllowUserToResizeRows = True
          End With
       End Sub
       Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
          'read from xml
          Dim dt As New DataTable
          dt.ReadXml("Test.xml")
          DataGridView1.DataSource = dt
       End Sub

       Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
          'Save To XML
          Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable)
          dt.AcceptChanges()
          dt.WriteXml("Test.xml", System.Data.XmlWriteMode.WriteSchema, False)
       End Sub
    • Marked as answer by 8Terrapin4 Thursday, November 10, 2011 1:42 PM
    Wednesday, November 9, 2011 6:04 PM
  • Button1 works fine.  Unfortunately, with Button2 I get: "DataTable does not support schema inference from Xml." and with Button3 nothing happens (i.e., no error either).

    Any further insight is appreciated.

    Regards.

    Did you copy the code as I posted?  It works fine on my end.  If the following does not help, please post the code you are testing.  Also please indicate which version of VB (2005, 2008, or 2010) you are using.  The version should not be an issue, I've successfully tested the sample I posted in all 3 versions, but you never know.

    Using the example as posted, try clicking on button1 and type in some data.  Click on Button3 to save it.  Then click on button1 again (clears Datagridview) then click Button2 (loads saved data).

    I can get the error message that you encountered is if the"System.Data.XmlWriteMode.WriteSchema" parameter is omitted from the Writexml statement. 

    Or are you trying to read in the xml file that you had in your original post?  If so, that xml does not contain any schema information and it would also create the error you encountered.

    Button3("Save") does not provide any user feedback.  It just saves the file to the program directory; in my case the "projName\bin\debug" folder.

     

     

     

    • Marked as answer by 8Terrapin4 Thursday, November 10, 2011 1:41 PM
    Wednesday, November 9, 2011 9:28 PM

All replies

  • dim ds as new dataset
    ds.Tables.Add(dt)
    ds.writeXML(TheFullFileName)
    
    
    Written direct in this message so watch typos.
    Success
    Cor
    • Edited by Cor Ligthert Wednesday, November 9, 2011 3:48 PM
    Wednesday, November 9, 2011 3:48 PM
  • Hi 8Terrapin4,

    I'm not sure what you are looking for, but it seems that you have most the basics presented and maybe just need some help with structuring your code to allow for:

        1. Create a blank Datagridview (you already have this code).
        2. Save entered data. (you already have the basic part of this.)
        3. Allow the user to load existing data and continuing editing it.

    So here is an example with three (3) buttons and a Datagridview on a form.  Button1 creates the blank entry Datagridview.  Button2 saves the data to an xml file.  Button3 loads an existing xml file.

    I leave it to you to handle the file name selection dialog and error handling.

       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          'create Table
          Dim dt As New DataTable("testtable")
          dt.Columns.Add("Department", GetType(String))
          dt.Columns.Add("Contact", GetType(String))
          dt.Columns.Add("Role", GetType(String))
          dt.Columns.Add("Expertise", GetType(String))

          With DataGridView1
             .DataSource = dt
             .AllowUserToAddRows = True : .AllowUserToDeleteRows = True
             .AllowUserToOrderColumns = False : .AllowUserToResizeRows = True
          End With
       End Sub
       Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
          'read from xml
          Dim dt As New DataTable
          dt.ReadXml("Test.xml")
          DataGridView1.DataSource = dt
       End Sub

       Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
          'Save To XML
          Dim dt As DataTable = CType(DataGridView1.DataSource, DataTable)
          dt.AcceptChanges()
          dt.WriteXml("Test.xml", System.Data.XmlWriteMode.WriteSchema, False)
       End Sub
    • Marked as answer by 8Terrapin4 Thursday, November 10, 2011 1:42 PM
    Wednesday, November 9, 2011 6:04 PM
  • TnTinMN,

    Thanks for the detailed steps as it helps me learn it.

    Button1 works fine.  Unfortunately, with Button2 I get: "DataTable does not support schema inference from Xml." and with Button3 nothing happens (i.e., no error either).

    Any further insight is appreciated.

    Regards.

    Wednesday, November 9, 2011 8:06 PM
  • Button1 works fine.  Unfortunately, with Button2 I get: "DataTable does not support schema inference from Xml." and with Button3 nothing happens (i.e., no error either).

    Any further insight is appreciated.

    Regards.

    Did you copy the code as I posted?  It works fine on my end.  If the following does not help, please post the code you are testing.  Also please indicate which version of VB (2005, 2008, or 2010) you are using.  The version should not be an issue, I've successfully tested the sample I posted in all 3 versions, but you never know.

    Using the example as posted, try clicking on button1 and type in some data.  Click on Button3 to save it.  Then click on button1 again (clears Datagridview) then click Button2 (loads saved data).

    I can get the error message that you encountered is if the"System.Data.XmlWriteMode.WriteSchema" parameter is omitted from the Writexml statement. 

    Or are you trying to read in the xml file that you had in your original post?  If so, that xml does not contain any schema information and it would also create the error you encountered.

    Button3("Save") does not provide any user feedback.  It just saves the file to the program directory; in my case the "projName\bin\debug" folder.

     

     

     

    • Marked as answer by 8Terrapin4 Thursday, November 10, 2011 1:41 PM
    Wednesday, November 9, 2011 9:28 PM
  • I got it working now... no need to get into the gory details but when I broke it back down per your last recommendations I saw what needed to be corrected on my end.

    Thanks for all of your time and help.

    Have a nice day!

    Thursday, November 10, 2011 1:41 PM
  • Hi - i have the same problem, I have used some of your code but what more do i need to fill in? It displays an error with: 

    dt.AcceptChanges()

    And what do you mean by this: "I leave it to you to handle the file name selection dialog and error handling."


    Tuesday, October 2, 2012 8:25 PM