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)?


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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"))
        dc = New DataColumn("FirstName", System.Type.GetType("System.String"))
        dc = New DataColumn("LastName", System.Type.GetType("System.String"))

        Dim dr As DataRow = dt.NewRow()
        dr(0) = 1
        dr(1) = "Fernando"
        dr(2) = "Soto"
        dr = dt.NewRow()
        dr(0) = 2
        dr(1) = "John"
        dr(2) = "Smith"
        dr = dt.NewRow()
        dr(0) = 3
        dr(1) = "Alice"
        dr(2) = "Wright"
        '' 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.

    End Sub
End Class

Open in new window

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.


Fernando SotoRetiredCommented:
        '' When ready to save the DataTable with its [b][u]update[/u][/b] jus do this.

Open in new window

Where dt in your code is your DataTable.
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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.
É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

vcharlesAuthor Commented:
Thank You.
Fernando SotoRetiredCommented:
Not a problem Victor, glad to help.
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.