Results 1 to 3 of 3

Thread: Save DataGridView to XML File Overwrites Existing

  1. #1

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Save DataGridView to XML File Overwrites Existing

    I am saving textbox text to a DGV and then saving the DGV row to a xml file but i cannot workout how to append instead of overwriting.

    Code:
        Public Sub WwriteToDataGridView()
            'save data to DGV
            With Ddata.DataGridView2
                'https://stackoverflow.com/questions/19162248/index-was-out-of-range-must-be-non-negative-And-less-than-the-size-of-the-colle
                ' Write to cell (0,0)
                '.ColumnCount = 6
                .Columns(0).Name = "REGO"
                .Rows(0).Cells(0).Value = OEM_Specs.TxReg.Text
                .Columns(1).Name = "WHEELBASE"
                .Rows(0).Cells(1).Value = OEM_Specs.TxtWheelBase.Text
                .Columns(2).Name = "REAR OVERHANG"
                .Rows(0).Cells(2).Value = OEM_Specs.TxtReOverhang.Text
                'gvm diff
                .Columns(3).Name = "GVM DIFF"
                .Rows(0).Cells(3).Value = TxtTBMeffectGVM.Text
                'tbw
                .Columns(4).Name = "TBM"
                .Rows(0).Cells(4).Value = TxtTowBallMassVan.Text
                'gvm gain/loss
                .Columns(5).Name = "GVM GAIN LOSS"
                Dim GVMVanEffect2 As Double = CDbl(TxtTBMeffectGVM.Text)
                Dim GgvmGainLoss As Double = TBM - GVMVanEffect2
                .Rows(0).Cells(5).Value = GgvmGainLoss.ToString
            End With
        End Sub
    Code:
     Public Sub SsaveDataGridViewToDataSet()
            Dim dt As DataTable = New DataTable("data")
            For i As Integer = 0 To Ddata.DataGridView2.ColumnCount - 1
                dt.Columns.Add(Ddata.DataGridView2.Columns(i).Name, GetType(System.String))
            Next
            Dim myrow As DataRow
            Dim icols As Integer = Ddata.DataGridView2.Columns.Count
            For Each drow As DataGridViewRow In Ddata.DataGridView2.Rows
                myrow = dt.NewRow()
                For i As Integer = 0 To icols - 1
                    myrow(i) = drow.Cells(i).Value
                Next
                dt.Rows.Add(myrow)
            Next
            dt.WriteXml(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\weights\data\data.xml")
        End Sub

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,518

    Re: Save DataGridView to XML File Overwrites Existing

    I don't think there is an append function. But you could just use ReadXML then add any new rows to the datatable then call WriteXml.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Save DataGridView to XML File Overwrites Existing

    I doubt append was implemented, as it would make little sense. An XML file is just a text file. It happens to hold the schema information for the data, along with the data itself, though. What does appending mean for that? It's just writing text, but that text is XML that may or may not match the schema of what is already there. If it doesn't, then you have a corrupted file. There doesn't appear to be any particularly safe means to implement an append in this case.

    For your typical text file, appending is simple. For XML, it isn't simple at all.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width