Help with saving DataGridView data to xml file

I am loading a DatagridView with data from a datatable, when I update the DatagridView how do I save the changes to an xml file using VB.NET (Windows App)?

Thanks,

Victor
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi Victor;

Here is some sample code on how it can bedone
Public Class Form1

    '' Class level variables
    Dim dt As New DataTable()
    '' Used to keep Datatable up to date
    Dim bs As New BindingSource()

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        '' Creating a DataTable objects and filling the table with data
        '' This is for test purposes only, your table will be created differently
        
        '' Gave a table name will be needed for serialization of the table to XML
        dt.TableName = "TheTable"
        Dim dc As New DataColumn("ID", System.Type.GetType("System.Int32"))
        dt.Columns.Add(dc)
        dc = New DataColumn("FirstName", System.Type.GetType("System.String"))
        dt.Columns.Add(dc)
        dc = New DataColumn("LastName", System.Type.GetType("System.String"))
        dt.Columns.Add(dc)

        Dim dr As DataRow = dt.NewRow()
        dr(0) = 1
        dr(1) = "Fernando"
        dr(2) = "Soto"
        dt.Rows.Add(dr)
        dr = dt.NewRow()
        dr(0) = 2
        dr(1) = "John"
        dr(2) = "Smith"
        dt.Rows.Add(dr)
        dr = dt.NewRow()
        dr(0) = 3
        dr(1) = "Alice"
        dr(2) = "Wright"
        dt.Rows.Add(dr)
        
        '' DataSource for the DataGridView and DataTable update
        bs.DataSource = dt
        DataGridView1.DataSource = bs

    End Sub
    
    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click

        '' When ready to save the DataTable with its update jus do this.
        dt.WriteXml("SavedToDisk.xml")

    End Sub
    
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Hi Fernando,

When I change the data in the Gnid and press Save, wil the changes be saved in he xml file? No access to app to  test your cide at the moment.

Thanks

V.
0
Fernando SotoRetiredCommented:
        '' When ready to save the DataTable with its [b][u]update[/u][/b] jus do this.
        dt.WriteXml("SavedToDisk.xml")

Open in new window


Where dt in your code is your DataTable.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

vcharlesAuthor Commented:
I will be loading the grid from an xml file, i think the key is to include  Dim bs As New BindingSource with the DataTable in order to save changes in Grid to the xml file.
0
Éric MoreauSenior .Net ConsultantCommented:
This is code I just written and tested:

    Private Sub btnGenerate_Click(sender As Object, e As EventArgs) Handles btnGenerate.Click
        'Create a table
        Dim dt As New DataTable("Person")
        With dt
            .Columns.Add("FirstName", GetType(String))
            .Columns.Add("LastName", GetType(String))


            'Add rows
            .LoadDataRow(New Object() {"Joe", "Dalton"}, True)
            .LoadDataRow(New Object() {"Jack", "Dalton"}, True)
            .LoadDataRow(New Object() {"Willam", "Dalton"}, True)
            .LoadDataRow(New Object() {"Averel", "Dalton"}, True)
        End With

        dt.WriteXml("data.xml", XmlWriteMode.WriteSchema)
    End Sub

    Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
        dim ds as New DataSet
        ds.ReadXml("data.xml", XmlReadMode.ReadSchema)
        DataGridView1.DataSource = ds.tables(0)
    End Sub

    Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        dim dt As DataTable = TryCast(DataGridView1.DataSource, DataTable)
        dt.WriteXml("data.xml", XmlWriteMode.WriteSchema)
    End Sub

Open in new window

0
vcharlesAuthor Commented:
Thank You.
0
Fernando SotoRetiredCommented:
Not a problem Victor, glad to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.