Question Using DataGridView to View/Edit XML

jsurpless

Well-known member
Joined
Jul 29, 2008
Messages
144
Programming Experience
Beginner
Hi all

I'm pretty new to VB.net programming - most of my experience is in VBA Office 2k3 - and I was hoping I've got a pretty easy question...

I've got an XML file that I'd like to display on a form... looking into this a bit, it seems that I'd want to use a DataGridView... so far, I've been able to read the XML into it but I can't quite figure out how to perform edits within the View and then write those out back to the XML...

Here's what I've done so far...

VB.NET:
    Private Sub MatchMakerXML_Editor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        XMLFilePath.Text = c_MatchMakerXML.XMLFilePath

        Dim MatchMakerXML_Dataset As New DataSet

        MatchMakerXML_Dataset.ReadXml(c_MatchMakerXML.XMLFilePath)

        XMLGridView.DataSource = MatchMakerXML_Dataset.Tables(0)

    End Sub

Can someone explain to me why reading the ".Tables(0)" is necessary? I've seen examples without it, so I'm curious... here's my XML file...

VB.NET:
<MATCHLIST>
<MATCH>
<AUTOCAD>UNIT</AUTOCAD>
<DATABASE>UNIT</DATABASE>
</MATCH>
<MATCH>
<AUTOCAD>UNIT</AUTOCAD>
<DATABASE>UNIT</DATABASE>
</MATCH>
</MATCHLIST>

Looking around, I've seen references to GridView_RowUpdating but I guess this doesn't apply to VB.Net?

Many thanks in advance!

-Justin
 
You don't need .Tables(0) in particular, but if data source is multitable like datasets are you have to specify which table by DataMember property, in this case table(0) is "MATCH".

To save changes get the data source object as DataSet and use WriteXml method. You could also keep the reference to this dataset object as a private class variable and use that instead of getting it from DataSource property like this:
VB.NET:
Dim ds As DataSet = CType(Me.DataGridView1.DataSource, DataSet)
ds.WriteXml("match2.xml")

GridView is an ASP.Net server control, DataGridView is what you are using in windows forms application.
 
Good Morning John

Thanks for the information...

If I understand you correctly, I can either use ".Tables(0)" (and not include .DataMember) OR omit the 1st part and specify the Datamember as the 'name' of the table I want to access?

Regarding your suggestion of writing back to XML... basically, you're just allow the user to make whatever changes to the data currently loaded in the DataGridView and then overwriting the XML with that information, correct? As such, there's no need to bother tracking the individual changes? But what if I want to read in only a portion of the XML file?

To save changes get the data source object as DataSet and use WriteXml method. You could also keep the reference to this dataset object as a private class variable and use that instead of getting it from DataSource property like this:

I understand what you're recommending (in principle) but I'm not sure that I follow your language... specifically, the 2nd sentence....

Thanks!
 
Yes, you can choose to use a table as datasource, or a set and specify which table in that set is bound.
what if I want to read in only a portion of the XML file?
If you meant 'display' then perhaps you can use a DataView and specify RowFilter. Else you have to load and save data manually with System.Xml tools.
I'm not sure that I follow
They call it "module level" in documentation, but most people would understand that "class level" is same scope. See Scope in Visual Basic
 
DataView is a class in System.Data namespace, look it up in help. All datatables have a DefaultView that is the default DataView used when bound to a view control.
 
did any of ye get that working doing the same myself need to pull info of a website thats in an xml format. can any of ye put some more code up to look at
 
would u mind posting up all the code u used have some of it done now but just need a few lines to finish it off.
 
Hi

I used this code to read the XML into a DataGridView

VB.NET:
        XMLFilePath.Text = c_MatchMakerXML.XMLFilePath

        Dim MatchMakerXML_Dataset As New DataSet

        MatchMakerXML_Dataset.ReadXml(c_MatchMakerXML.XMLFilePath)

        XMLGridView.DataSource = MatchMakerXML_Dataset
        XMLGridView.DataMember = "MATCH"

and then I used this code (based on John's recommendations)

VB.NET:
        Dim MatchMakerXML_Dataset As New DataSet

        MatchMakerXML_Dataset = CType(XMLGridView.DataSource, DataSet)

        MatchMakerXML_Dataset.WriteXml(c_MatchMakerXML.XMLFilePath)

Hope that helps...
 
Oh yes... sorry about that... it's just a class module I've developed to store stuff with XML... I'm not even sure if I really need to use it; rest assured that I'm just reading a string with that particular statement...
 
A slight spin to this original question...

I added a 2nd GridView on this to display data from another table in the same XML file...

VB.NET:
            XMLFilePath.Text = c_MatchMakerXML.XMLFilePath

            Dim MatchMakerXML_Dataset As New DataSet

            MatchMakerXML_Dataset.ReadXml(c_MatchMakerXML.XMLFilePath)

            ComponentTable_Matches_GridView.DataSource = MatchMakerXML_Dataset
            ComponentTable_Matches_GridView.DataMember = "COMPONENT_MATCH"

            DrawingTable_Matches_GridView.DataSource = MatchMakerXML_Dataset
            DrawingTable_Matches_GridView.DataMember = "DRAWING_MATCH"

Now here's the code where I am saving the GridView back to the XML...

VB.NET:
                MatchMakerXML_Dataset = CType(ComponentTable_Matches_GridView.DataSource, DataSet)


                'Commit MatchMakerXML
                MatchMakerXML_Dataset.WriteXml(c_MatchMakerXML.XMLFilePath)

As you can see, I am only retrieving ONE of the gridviews; yet, I have found that changes made to the other gridview are also written to the XML file...

Can someone explain why this is happening?

Thanks!

-Justin
 
When you assign Dataset to Datasource only a reference is set, so they both point to the same dataset, whichever reference you use you get the same object. Read help topic Value Types and Reference Types.
 
Back
Top