|
-
Oct 3rd, 2010, 01:58 PM
#1
Thread Starter
New Member
XML and Datagridview
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 onlu to the same columns, without changing the rest of the datagridview.
Code used to create XML file:
Code:
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:
Code:
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:
Code:
DataGridView1.DataSource = ds.Tables(0)
Last edited by Quay; Oct 4th, 2010 at 07:22 AM.
-
Oct 3rd, 2010, 06:18 PM
#2
Re: XML and Datagridview
Try setting AutoGenerateColumns property of the DataGridView to False as shown below.
Code:
DataGridView1.AutoGenerateColumns = False
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)
-
Oct 4th, 2010, 04:52 AM
#3
Thread Starter
New Member
Re: XML and Datagridview
Thanks for your suggestion, I tried it out, but it does not do the whole job.
It does not add more columns, but it does not add my XML data to my datagridview. The only thing that happens is that I get the correct amount of rows in my datagridview, but unfortunate all rows are empty, and I have no data in my datagridview.
-
Oct 5th, 2010, 05:02 AM
#4
Thread Starter
New Member
Re: XML and Datagridview
Hello,
is there anyone that may have som ideas for how I can solve this problem?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|