Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datagrids and XML 1

Status
Not open for further replies.

SiJP

Programmer
May 8, 2002
708
GB
I have an XML file that contains.

<Data>
<Employees>
<Employee>
<FirstName>joe</FirstName>
<Surname>bloggs</Surname>
</Employee>
<Employee>
<FirstName>harry</FirstName>
<Surname>potter</Surname>
</Employee>
<Employees>
<Sites>
<Site>
<SiteName>London</SiteName>
</Site>
</Sites>
</Data>

I also have created a form, with a combo box on it with two values ("Employees" and "Sites") and a datagrid.

The idea being that when the user selects an entry in the combo, the datagrid fills with the relevant details from the xml. For example a user selects 'Employees', the datagrid shows:

FirstName | Surname
--------------------
joe | bloggs
harry | potter

etc..

If a change is made to these details, I need to update the xml file.

I'm struggling on two items, which I would like some help on!:

- Once I've populated the dataset with the xml, how do I then only load the xml into the datagrid, based on the combo selection? e.g. if "Employees" is selected, only should information for each <employee> in <employees>...

- If data is changed in the grid, will I be able to simply update the xml from the dataset (presuming the dataset in bound to the datagrid).

Thanks guys!

------------------------
Hit any User to continue
 
Hi SiJP

A couple of things (you probably know this but I'll mention it anyways)...

1.)The XML you posted is incorrect. You are missing the '/' on the closing Employees tag.

2.) Assuming you have read in the Xml so there are 2 tables in the dataset (Employees and Sites), you can adjust the Datagrid's DataMember property in the SelectedIndexChanged event of the combobox

DataGrid1.DataMember = Me.ComboBox1.SelectedItem.ToString

3.) As for updating the actual Xml file, I would just use the dataset WriteXml method. So on a click event or something,

DataSet1.WriteXml( "dataset.xml") 'whatever file you used to load it.

Hope This Helps,

------------------------------------------
Steen Bray
Kermode Computing Solutions
 
Cheers Steen

1) Yeah the missing '/' is a typo. That's what I get for typing it in here directly!

2) If I enter "Employees" as the datamember, nothing shows in the grid. If I enter "Employee" as the datamember, only the first employee shows.

3) Haven't got to this bit yet - still trying to populate datagrid ;)

------------------------
Hit any User to continue
 
If you are using DataSet1.ReadXml( "dataset.xml" ), it's going to read in 4 seperate tables.

If you take out the Parent Employees & Sites, and just have the instances of the Employees and Sites, it will read in 2 tables, Employee (with 2 records) and Site (1 record).

From there, you can set the Datasource property to DataSet1.

Here's the code I used to test your problem out:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Me.DataSet1.ReadXml("dataset.xml")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try

Dim tbl As DataTable

'load dataset into combo box
For Each tbl In DataSet1.Tables
Me.ComboBox1.Items.Add(tbl.TableName)
Next

'Init Datagrid
Me.DataGrid1.DataSource = Me.DataSet1
Me.DataGrid1.DataMember = Me.DataSet1.Tables(0).TableName

End Sub

and the Xml File after I edited it....
<?xml version="1.0" encoding="utf-8" ?>
<Data>
<Employee>
<FirstName>joe</FirstName>
<Surname>bloggs</Surname>
</Employee>
<Employee>
<FirstName>harry</FirstName>
<Surname>potter</Surname>
</Employee>
<Site>
<SiteName>London</SiteName>
</Site>
</Data>

If you leave the Xml File in it's original format, the code still works, however you have 4 tables: Employees, Employee, Sites, Site. The ComboBox event will still fire, however, the selection of Sites and Employees will only have links to Site and Employee respectively.

I hope this helps you out.

------------------------------------------
Steen Bray
Kermode Computing Solutions
 
Ah, got it working now, thanks for that!

------------------------
Hit any User to continue
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top