Write Datagridview data to xml and retrieve

bones

Well-known member
Joined
Aug 23, 2014
Messages
143
Programming Experience
Beginner
I have looked at and experimented with a couple of code examples to do this but can't get it quite right. I hate to bother people with things I can do on my own but in this instance I really could use some help because VB2010 is new to me.

I have a datagridview1 on a form plus several textboxes that are used for data calculations.

When the user has performed the calculations to their satisfaction, there is a Button that writes the calculated data to the datagridview1. I would like to keep that button function intact.

What I need to accomplish is saving datagridview1 data and retrieving it. With my limited VB experience it looks to me like writing the data to xml is the correct method. I should then be able to retrieve the data back to the datagridview1 when the form loads so that the user can either add more rows or edit current rows and re-save the data.

May I please have some assistance with this?

I fiddled with this code but can't get the syntax correct to make VB2010 happy....believe me...I fiddled with this for quite a while before I broke down and asked for help

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim gridtable As DataTable = New DataTable(datagridtable")
Dim gridtable_collumn1 As DataColumn = New _ DataColumn("datagridcollumn1") ' [COLOR=#ff0000]I tried changing "datagridcollumnn1" to column1 name of my datagrid[/COLOR]
Dim gridtable_collumn2 As DataColumn = New _ DataColumn("datagridcollumn2")


 gridtable.Columns.Add(gridtable_collumn1) '[COLOR=#ff0000] and also changed it here[/COLOR]
 gridtable.Columns.Add(gridtable_collumn2)


 Dim gridrow As DataGridViewRow
 Dim table_row As DataRow


  For Each gridrow In datagridview1.Rows
    table_row = gridtable .NewRow
     table_row ("datagridcollumn1")=gridrow.Cells("collumn1").Value
     table_row ("datagridcollumn2")=gridrow.Cells("collumn2").Value
     gridtable.Rows.Add( table_row)
  gridtable.WriteXml("test.xml")
next gridrow
 
Last edited:
You should create your DataTable upfront and then bind it to the grid before any data is created. That way, anything the user does will affect the DataTable directly. Once the user has finished with the data, you simply call WriteXml on the DataTable and you're done; there's no need to move any data around because data-binding has already done that for you. If you need to load data then you simply call ReadXml on a DataTable and bind that to the grid. E.g.
Public Class Form1

    Private table As New DataTable

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With Me.table.Columns
            .Add("ID", GetType(Integer))
            .Add("Name", GetType(String))
        End With

        Me.DataGridView1.DataSource = Me.table
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.table.WriteXml("file path here")
    End Sub

End Class
Note that binding the DataTable to the DataGridView is going to automatically generate the columns in the grid.
 
Back
Top