Victor Charles
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_I D")
dtLink.Columns.Add("WDevel oper_ID")
dtLink.Columns.Add("WUser_ ID")
dtLink.Columns.Add("DInven tory_ID")
'2
Dim linker As XElement = XElement.Load(Application. StartupPat h + "\Link.xml")
'3
For Each item As XElement In linker.Elements("Row")
Dim linkID As String = item.Element("Link_ID").Va lue
Dim CtryDeveloperID As String = item.Element("WDeveloper_I D").Value
Dim CtryUserID As String = item.Element("WUser_ID").V alue
Dim DInventoryID As String = item.Element("DInventory_I D").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.AlternatingR ows = True
C1TrueDBGrid1.Caption = "Inventory Charts"
Me.C1TrueDBGrid1.Splits(0) .DisplayCo lumns(0).W idth = Me.C1TrueDBGrid1.Splits(0) .DisplayCo lumns(0).V isible = False
Dim vReceiverDev As C1.Win.C1TrueDBGrid.ValueI temCollect ion
Dim vReceiverUsr As C1.Win.C1TrueDBGrid.ValueI temCollect ion
vReceiverDev = Me.C1TrueDBGrid1.Columns(" WDeveloper _ID").Valu eItems.Val ues
vReceiverUsr = Me.C1TrueDBGrid1.Columns(" WUser_ID") .ValueItem s.Values
Dim WCtryDeveloper As XElement = XElement.Load(Application. StartupPat h + "\Receiver.xml")
For Each item As XElement In WCtryDeveloper.Elements("R ow")
Dim receiverID As String = item.Element("Receiver_ID" ).Value
Dim receiverName As String = item.Element("Receiver").V alue
vReceiverDev.Add(New C1.Win.C1TrueDBGrid.ValueI tem(receiv erID, receiverName))
vReceiverUsr.Add(New C1.Win.C1TrueDBGrid.ValueI tem(receiv erID, receiverName))
Next
Me.C1TrueDBGrid1.Columns(" WDeveloper _ID").Valu eItems.Tra nslate = True
Me.C1TrueDBGrid1.Columns(" WDeveloper _ID").Valu eItems.Pre sentation = C1.Win.C1TrueDBGrid.Presen tationEnum .ComboBox
Me.C1TrueDBGrid1.Columns(" WUser_ID") .ValueItem s.Translat e = True
Me.C1TrueDBGrid1.Columns(" WUser_ID") .ValueItem s.Presenta tion = C1.Win.C1TrueDBGrid.Presen tationEnum .ComboBox
Dim vInventory As C1.Win.C1TrueDBGrid.ValueI temCollect ion
vInventory = Me.C1TrueDBGrid1.Columns(" DInventory _ID").Valu eItems.Val ues
Dim DInventory As XElement = XElement.Load(Application. StartupPat h + "\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.ValueI tem(invent oryID, inventoryName))
Next
Me.C1TrueDBGrid1.Columns(" DInventory _ID").Valu eItems.Tra nslate = True
Me.C1TrueDBGrid1.Columns(" DInventory _ID").Valu eItems.Pre sentation = C1.Win.C1TrueDBGrid.Presen tationEnum .ComboBox
Section B.
Try
Dim ds As New DataSet
ds.ReadXml(Application.Sta rtupPath + "\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.St artupPath + "\Link.xml")
Catch ex As Exception
MsgBox("Items selected were not saved", MsgBoxStyle.OkOnly, "Confirmation")
Exit Sub
End Try
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_I
dtLink.Columns.Add("WDevel
dtLink.Columns.Add("WUser_
dtLink.Columns.Add("DInven
'2
Dim linker As XElement = XElement.Load(Application.
'3
For Each item As XElement In linker.Elements("Row")
Dim linkID As String = item.Element("Link_ID").Va
Dim CtryDeveloperID As String = item.Element("WDeveloper_I
Dim CtryUserID As String = item.Element("WUser_ID").V
Dim DInventoryID As String = item.Element("DInventory_I
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.AlternatingR
C1TrueDBGrid1.Caption = "Inventory Charts"
Me.C1TrueDBGrid1.Splits(0)
Dim vReceiverDev As C1.Win.C1TrueDBGrid.ValueI
Dim vReceiverUsr As C1.Win.C1TrueDBGrid.ValueI
vReceiverDev = Me.C1TrueDBGrid1.Columns("
vReceiverUsr = Me.C1TrueDBGrid1.Columns("
Dim WCtryDeveloper As XElement = XElement.Load(Application.
For Each item As XElement In WCtryDeveloper.Elements("R
Dim receiverID As String = item.Element("Receiver_ID"
Dim receiverName As String = item.Element("Receiver").V
vReceiverDev.Add(New C1.Win.C1TrueDBGrid.ValueI
vReceiverUsr.Add(New C1.Win.C1TrueDBGrid.ValueI
Next
Me.C1TrueDBGrid1.Columns("
Me.C1TrueDBGrid1.Columns("
Me.C1TrueDBGrid1.Columns("
Me.C1TrueDBGrid1.Columns("
Dim vInventory As C1.Win.C1TrueDBGrid.ValueI
vInventory = Me.C1TrueDBGrid1.Columns("
Dim DInventory As XElement = XElement.Load(Application.
For Each item As XElement In DInventory.Elements("Row")
Dim inventoryID As String = item.Element("Inventory_ID
Dim inventoryName As String = item.Element("Inventory").
vInventory.Add(New C1.Win.C1TrueDBGrid.ValueI
Next
Me.C1TrueDBGrid1.Columns("
Me.C1TrueDBGrid1.Columns("
Section B.
Try
Dim ds As New DataSet
ds.ReadXml(Application.Sta
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).
r!WUser_ID = C1TrueDBGrid43.Columns(0).
r!DInventory_ID = C1TrueDBGrid44.Columns(0).
dt.Rows.Add(r)
ds.WriteXml(Application.St
Catch ex As Exception
MsgBox("Items selected were not saved", MsgBoxStyle.OkOnly, "Confirmation")
Exit Sub
End Try
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Problem solved.
ASKER
Thank You
ASKER
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.
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