CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2018
    Posts
    4

    [RESOLVED] Unbuond DataGridView does not save new row in XML file

    Hi everybody. Hope somebody can help me with this problem that is causing me some headache:
    I use a DataGridView loading data from an XML file (using a DataSet). When I try to save data to XML during FormClosing event it writes all except for the current row: if it is a new row, it is not added to my file, while if I am editing an already existing row, it does not write the modified text.

    This is my (stupid) XML file:
    Code:
    <?xml version="1.0" standalone="yes"?>
    <DataSetParametri>
      <TableParametri>
        <Nome>AAAA</Nome>
        <Parametri>AAAA</Parametri>
      </TableParametri>
      <TableParametri>
        <Nome>BBBB</Nome>
        <Parametri>BBBB</Parametri>
      </TableParametri>
      <TableParametri>
        <Nome>CCCC</Nome>
        <Parametri>CCCC</Parametri>
      </TableParametri>
    </DataSetParametri>
    This is the code I used for loading the XML file in DataGridView1 datagrid through DataSetParametri DataSet:
    Code:
        Private Sub Parametri_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
                DataSetParametri.ReadXml("c:\circolo\parametri.xml")
                DataGridView1.DataSource = DataSetParametri
                DataGridView1.DataMember = "TableParametri"
    
        End Sub
    This is the code I used to save the DataGridView1 data back to XML file when I am closing the form:
    Code:
        Private Sub Parametri_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    
                DataSetParametri = DirectCast(DataGridView1.DataSource, DataSet)
                DataSetParametri.WriteXml("c:\circolo\parametri.xml", XmlWriteMode.IgnoreSchema)
    
        End Sub
    What's wrong in my code? Why it does not update the file with the last editing operation?
    Thanks for your help.

  2. #2
    Join Date
    Nov 2018
    Posts
    4

    Re: Unbuond DataGridView does not save new row in XML file

    Ok I am trying to introduce a BindingSource as suggested by a skilled programmer, but I obtain the same problem. Why???? How can I solve?
    Anybody can help me? Thanks in advance

    Code:
    Private Sub Parametri_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
                DataSetParametri.ReadXml("c:\circolo\parametri.xml")
                BindingSource2.DataSource = DataSetParametri
                DataGridView1.DataSource = BindingSource2
                DataGridView1.DataMember = "TableParametri"
    
    End Sub

    Code:
    Private Sub Parametri_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    
                BindingSource2.EndEdit()
                DataSetParametri = DirectCast(BindingSource2.DataSource, DataSet)
                DataSetParametri.WriteXml("c:\circolo\parametri.xml", XmlWriteMode.IgnoreSchema)
    
    End Sub

  3. #3
    Join Date
    Nov 2018
    Posts
    4

    Re: Unbuond DataGridView does not save new row in XML file

    Ok I found the solution: BindingSource is not essential in my case, I can use the first way I published.
    The problem is solved using a Form Validate call in this way:

    Code:
    Private Sub Parametri_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    
                Me.Validate()
                DataSetParametri.WriteXml("c:\circolo\parametri.xml", XmlWriteMode.IgnoreSchema)
    
    End Sub

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

Featured