Link to home
Start Free TrialLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

How update xml file based on values selected from a data grid?

Subject:  How update xml file based on values selected from a data grid?

Hello,

I’m using the code in below (Section A) to update data in my “link.xml” using a combo box with my grid control, the problem is I can’t figure out how to save the data modified to the xml file, below (Section B) is the code I use to save new data to the xml file, can you please help me modify the code in section B to only save the rows modified?

Thanks,

Victor

Section A.

dtLink = New DataTable("Link")
        dtLink.Columns.Add("Link_ID")
        dtLink.Columns.Add("WDeveloper_ID")
        dtLink.Columns.Add("WUser_ID")
        dtLink.Columns.Add("DInventory_ID")


        '2
        Dim linker As XElement = XElement.Load(Application.StartupPath + "\Link.xml")
        '3
        For Each item As XElement In linker.Elements("Row")
            Dim linkID As String = item.Element("Link_ID").Value
            Dim CtryDeveloperID As String = item.Element("WDeveloper_ID").Value
            Dim CtryUserID As String = item.Element("WUser_ID").Value
            Dim DInventoryID As String = item.Element("DInventory_ID").Value
 
            Dim drLink As DataRow = dtLink.NewRow()
            drLink("Link_ID") = linkID
            drLink("WDeveloper_ID") = CtryDeveloperID
            drLink("WUser_ID") = CtryUserID
            drLink("DInventory_ID") = DInventoryID
            dtLink.Rows.Add(drLink)
        Next

        Dim bs As New BindingSource()
        bs.DataSource = dtLink
        C1TrueDBGrid1.DataSource = bs
        C1TrueDBGrid1.AlternatingRows = True
        C1TrueDBGrid1.Caption = "Inventory Charts"
        Me.C1TrueDBGrid1.Splits(0).DisplayColumns(0).Width = Me.C1TrueDBGrid1.Splits(0).DisplayColumns(0).Visible = False

        Dim vReceiverDev As C1.Win.C1TrueDBGrid.ValueItemCollection
        Dim vReceiverUsr As C1.Win.C1TrueDBGrid.ValueItemCollection
        vReceiverDev = Me.C1TrueDBGrid1.Columns("WDeveloper_ID").ValueItems.Values
        vReceiverUsr = Me.C1TrueDBGrid1.Columns("WUser_ID").ValueItems.Values

        Dim WCtryDeveloper As XElement = XElement.Load(Application.StartupPath + "\Receiver.xml")

        For Each item As XElement In WCtryDeveloper.Elements("Row")
            Dim receiverID As String = item.Element("Receiver_ID").Value
            Dim receiverName As String = item.Element("Receiver").Value

            vReceiverDev.Add(New C1.Win.C1TrueDBGrid.ValueItem(receiverID, receiverName))
            vReceiverUsr.Add(New C1.Win.C1TrueDBGrid.ValueItem(receiverID, receiverName))
        Next

        Me.C1TrueDBGrid1.Columns("WDeveloper_ID").ValueItems.Translate = True
        Me.C1TrueDBGrid1.Columns("WDeveloper_ID").ValueItems.Presentation = C1.Win.C1TrueDBGrid.PresentationEnum.ComboBox
        Me.C1TrueDBGrid1.Columns("WUser_ID").ValueItems.Translate = True
        Me.C1TrueDBGrid1.Columns("WUser_ID").ValueItems.Presentation = C1.Win.C1TrueDBGrid.PresentationEnum.ComboBox

        Dim vInventory As C1.Win.C1TrueDBGrid.ValueItemCollection
        vInventory = Me.C1TrueDBGrid1.Columns("DInventory_ID").ValueItems.Values

        Dim DInventory As XElement = XElement.Load(Application.StartupPath + "\Inventory.xml")

        For Each item As XElement In DInventory.Elements("Row")
            Dim inventoryID As String = item.Element("Inventory_ID").Value
            Dim inventoryName As String = item.Element("Inventory").Value

            vInventory.Add(New C1.Win.C1TrueDBGrid.ValueItem(inventoryID, inventoryName))

        Next

        Me.C1TrueDBGrid1.Columns("DInventory_ID").ValueItems.Translate = True
        Me.C1TrueDBGrid1.Columns("DInventory_ID").ValueItems.Presentation = C1.Win.C1TrueDBGrid.PresentationEnum.ComboBox





Section B.


Try
            Dim ds As New DataSet
            ds.ReadXml(Application.StartupPath + "\Link.xml")
            Dim dt As DataTable = ds.Tables(0)
            Dim r As DataRow = dt.NewRow
            r!Link_ID = dt.Rows(dt.Rows.Count - 1)("Link_ID") + 1
            r!WDeveloper_ID = C1TrueDBGrid42.Columns(0).Value
            r!WUser_ID = C1TrueDBGrid43.Columns(0).Value
            r!DInventory_ID = C1TrueDBGrid44.Columns(0).Value
            dt.Rows.Add(r)
            ds.WriteXml(Application.StartupPath + "\Link.xml")

        Catch ex As Exception
            MsgBox("Items selected were not saved", MsgBoxStyle.OkOnly, "Confirmation")
            Exit Sub
        End Try

Avatar of Victor  Charles
Victor Charles
Flag of United States of America image

ASKER

Hello again,

I tried the code below, but receiving the following error on the last "Next" Any ideas on how to fix this error?

Error: Collection was modified; enumeration operation might not execute.

Code:
Dim linker As XElement = XElement.Load(Application.StartupPath + "\Link.xml")
For Each dr As DataRow In dtLink.Rows
Dim row As String
row = String.Format("{0} - {1} - {2} - {3}", dr(0), dr(1), dr(2), dr(3))
'MessageBox.Show(row)
For Each item As XElement In linker.Elements("Row")
Dim drLink As DataRow = dtLink.NewRow()
drLink("Link_ID") = dr(0)
drLink("WDeveloper_ID") = dr(1)
drLink("WUser_ID") = dr(2)
drLink("DInventory_ID") = dr(3)
dtLink.Rows.Add(drLink)
Next
Next 'ERROR Line

Thanks,

Victor

Avatar of Nasir Razzaq
The error you get in above row is because you are looping through rows and adding items to the rows collection so the loop bounds get changed.
ASKER CERTIFIED SOLUTION
Avatar of Victor  Charles
Victor Charles
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Problem solved.
Thank You