Hello, I hope someone can give me some help.
I have a datagridview (Unbound), wich calculate several numbers, and I use this following code to save 5 of the columns to a XML file.

My problem is that I would like to load this file back to my datagridview, and only to the same columns, without changing the rest of the datagridview.

Code used to create XML file:

Dim gridtable As DataTable = New DataTable("WaypointLeg")
        Dim gridtable_collumn1 As DataColumn = New DataColumn("column1")
        Dim gridtable_collumn11 As DataColumn = New DataColumn("column11")
        Dim gridtable_collumn18 As DataColumn = New DataColumn("column18")
        Dim gridtable_collumn19 As DataColumn = New DataColumn("column19")
        Dim gridtable_collumn20 As DataColumn = New DataColumn("column20")
        Dim name As String
        name = IDTextBox.Text
        gridtable.Columns.Add(gridtable_collumn1)
        gridtable.Columns.Add(gridtable_collumn11)
        gridtable.Columns.Add(gridtable_collumn18)
        gridtable.Columns.Add(gridtable_collumn19)
        gridtable.Columns.Add(gridtable_collumn20)
        Dim gridrow As DataGridViewRow
        Dim table_row As DataRow
        For Each gridrow In DataGridView1.Rows
            table_row = gridtable.NewRow
            table_row("column1") = gridrow.Cells("column1").Value
            table_row("column11") = gridrow.Cells("column11").Value
            table_row("column18") = gridrow.Cells("column18").Value
            table_row("column19") = gridrow.Cells("column19").Value
            table_row("column20") = gridrow.Cells("column20").Value
            gridtable.Rows.Add(table_row)
            gridtable.WriteXml(Application.StartupPath & "\Data\" & name & ".xml")
        Next gridrow

I have tried with the following code but when I use this, the data from my XML file appear at the end of my datagridview (Adding 5 more columns to the end). I would like to "update" the same columns...

Code I use to Load XML file:

Dim name As String
        name = IDTextBox.Text
        Dim xmlFile As XmlReader
        xmlFile = XmlReader.Create(Application.StartupPath & "\Data\" & Name & ".xml", New XmlReaderSettings())
        Dim ds As New DataSet
        ds.ReadXml(xmlFile)
        DataGridView1.DataSource = ds.Tables(0)

Anyone knows how I can load my XML file, and only update certain columns in my datagridview and not add 5 "more" columns to my datagridview?

Is there a way to load the XML data to my datagridview without binding the datagridview to the XML file?

I mean without the use of the:

DataGridView1.DataSource = ds.Tables

Of Course you can load it using some thing like:

For RowNum as Integer = 0 to ds.Tables(0).Rows.Count -1
    Dim CurrRow as DataRow = ds.Tables(0).Rows(RowNum)
    '
    ' Assuming the data Grid View has enough rows
    '
    DataGridView1.Rows(RowNum).Cells("column1").Value = Currrow("column1").Value
    DataGridView1.Rows(RowNum).Cells("column11").Value = Currrow("column11").Value
    
    . etc

Next

Hope this helps

Thank you lolafuertes, this was just what I was looking for, I just had to change the Currrow("column1").Value to ToString, like:

DataGridView1.Rows(RowNum).Cells("Column1").Value = CurrRow("Column1").ToString

But this did the job exsactly the way I wanted.
Thank you.

Hello everyone.
Sorry i'm french man and my english is not yet perfect :)
I just followed your topic because i'm in the same case, and i succeded , but now i want to fill my datagrdiview with the XML file.

here is the code i adapted to yours to success the record of my datagridview data in the xml file, i have 9 collumns :

Dim datactions1 As DataTable = New DataTable("tableactions")
Dim ds As DataSet = New DataSet
Dim datactions1_collumn1 As DataColumn = New DataColumn("CODE")
Dim datactions1_collumn2 As DataColumn = New DataColumn("DESIGNATION")
Dim datactions1_collumn3 As DataColumn = New DataColumn("QUANTITE")
Dim datactions1_collumn4 As DataColumn = New DataColumn("PRIX")
Dim datactions1_collumn5 As DataColumn = New DataColumn("ESTIMATION")
Dim datactions1_collumn6 As DataColumn = New DataColumn("COURS")
Dim datactions1_collumn7 As DataColumn = New DataColumn("VALORISATION")
Dim datactions1_collumn8 As DataColumn = New DataColumn("PLUSVALUECFA")
Dim datactions1_collumn9 As DataColumn = New DataColumn("PLUSVALUEPCT")
'Dim name As String
datactions1.Columns.Add(datactions1_collumn1)
datactions1.Columns.Add(datactions1_collumn2)
datactions1.Columns.Add(datactions1_collumn3)
datactions1.Columns.Add(datactions1_collumn4)
datactions1.Columns.Add(datactions1_collumn5)
datactions1.Columns.Add(datactions1_collumn6)
datactions1.Columns.Add(datactions1_collumn7)
datactions1.Columns.Add(datactions1_collumn8)
datactions1.Columns.Add(datactions1_collumn9)

Dim action1row As DataGridViewRow
Dim act1table_row As DataRow
For Each action1row In DataGridView1.Rows
act1table_row = datactions1.NewRow
act1table_row("CODE") = action1row.Cells("CODE").Value
act1table_row("DESIGNATION") = action1row.Cells("DESIGNATION").Value
act1table_row("QUANTITE") = action1row.Cells("QUANTITE").Value
act1table_row("PRIX") = action1row.Cells("PRIX").Value
act1table_row("ESTIMATION") = action1row.Cells("ESTIMATION").Value
act1table_row("COURS") = action1row.Cells("COURS").Value
act1table_row("VALORISATION") = action1row.Cells("VALORISATION").Value
act1table_row("PLUSVALUECFA") = action1row.Cells("PLUSVALUECFA").Value
act1table_row("PLUSVALUEPCT") = action1row.Cells("PLUSVALUEPCT").Value

datactions1.Rows.Add(act1table_row)
ds.WriteXml(filePath)

Next action1row

MsgBox("datagridview recorded!!!", vbInformation)

Can you help me by giving me a way to recover the xml file with a clik on a button, wich code must i write according to my case?

i tried follow the answer of lolafuertes, but i don't understand everything, so my code doesn't work.

please help me !!! :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.