Adding data to listview from xml to two columns

RoyLittle0

Active member
Joined
Nov 17, 2012
Messages
38
Location
Derby, Uk
Programming Experience
Beginner
How can i add data to a listview from a xml file, i want to add two columns to the list view, i can add data to a listbox but i cant seem to do the same to a listview, the data i need to add is from a xml file, which is "id" and "Name" in two separate columns, they are part of a cascading set of 12 listview boxes each dependent on the one above and i need the id to implement the next cascade.

There are 12 list views in total, so i assume i need to define what goes into each of the columns, can this be done globally for all listviews?

The below code has been modified for listboxes to give me the additional column but i cant seem to get it to work in listview

any pointers would be appreciated

VB.NET:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBoxes.AddRange({Solution1, Solution2, Solution3, Solution4, Solution5, Solution6, Solution7, Solution8, Solution9, Solution10, Solution11, Solution12}) 'this has to be done here to ensure that the controls have initialised


        doc = XDocument.Load(IO.Path.Combine(Application.StartupPath, "Test.xml"))
        ' This will fill solution box 1 with the Root 1 information from the xml file
        Dim Root As New ListViewItem
            (
        From A In doc...<Section1>
        Select Section1 = A.@id()
            ).ToArray


        Solution1.Items.Add(Root)


    End Sub


    Private Sub Sloution1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


        Select Case ListBoxes.IndexOf(sender)
            'Solution1.click event, fills Solution box 2 with values based on the information in Solution box 1
            Case 0
                Dim Root2 =
                (
                    From a In doc...<Section1>
                    Where a.@id = Solution1.Text
                    From b In a.<Root2>.<Section2>
                    Select b.@id
                ).ToArray


                Solution2.Items.Add(Root2)
                'Solution2.click event, fills Solution box 3 with values based on the information in Solution box 2
            Case 1
                If Solution2.Items.Count > 0 Then
                    Dim Root3 =
                    (
                    From b In doc...<Section2>
                    Where b.@id = (Solution2.Text)
                    From c In b.<Root3>.<Section3>
                    Select c.@id
                    ).ToArray


                    Solution3.Items.Add(Root3)
                End If
                'Solution3.click event, fills Solution box 4 with values based on the information in Solution box 3
            Case 2
                If Solution3.Items.Count > 0 Then
                    Dim Root4 =
                    (
                    From c In doc...<Section3>
                    Where c.@id = Solution3.Text
                    From d In c.<Root4>.<Section4>
                    Select d.@id
                    ).ToArray


                    Solution4.Items.Add(Root4)
                End If
                'Solution4.click event, fills Solution box 5 with values based on the information in Solution box 4
            Case 3
                If Solution4.Items.Count > 0 Then
                    Dim Root5 =
                    (
                    From d In doc...<Section4>
                    Where d.@id = Solution4.Text
                    From f In d.<Root5>.<Section5>
                    Select f.@id
                    ).ToArray


                    Solution5.Items.Add(Root5)
                End If
                'Solution5.click event, fills Solution box 6 with values based on the information in Solution box 5
            Case 4
                If Solution5.Items.Count > 0 Then
                    Dim Root6 =
                    (
                    From f In doc...<Section5>
                    Where f.@id = Solution5.Text
                    From g In f.<Root6>.<Section6>
                    Select g.@id
                    ).ToArray


                    Solution6.Items.Add(Root6)
                End If
                'Solution6.click event, fills Solution box 7 with values based on the information in Solution box 6
            Case 5
                If Solution6.Items.Count > 0 Then
                    Dim Root7 =
                    (
                    From g In doc...<Section6>
                    Where g.@id = Solution6.Text
                    From h In g.<Root7>.<Section7>
                    Select h.@id
                    ).ToArray


                    Solution7.Items.Add(Root7)
                End If
                'Solution7.click event, fills Solution box 8 with values based on the information in Solution box 7
            Case 6
                If Solution7.Items.Count > 0 Then
                    Dim Root8 =
                    (
                    From h In doc...<Section7>
                    Where h.@id = Solution7.Text
                    From i In h.<Root8>.<Section8>
                    Select i.@id
                    ).ToArray


                    Solution8.Items.Add(Root8)
                End If
                'Solution8.click event, fills Solution box 9 with values based on the information in Solution box 8
            Case 7
                If Solution8.Items.Count > 0 Then
                    Dim Root9 =
                    (
                    From i In doc...<Section8>
                    Where i.@id = Solution8.Text
                    From j In i.<Root9>.<Section9>
                    Select j.@id
                    ).ToArray


                    Solution9.Items.Add(Root9)
                End If
                'Solution9.click event, fills Solution box 10 with values based on the information in Solution box 9
            Case 8
                If Solution9.Items.Count > 0 Then
                    Dim Root10 =
                    (
                    From j In doc...<Section9>
                    Where j.@id = Solution9.Text
                    From k In j.<Root10>.<Section10>
                    Select k.@id
                    ).ToArray


                    Solution10.Items.Add(Root10)
                End If
                'Solution10.click event, fills Solution box 11 with values based on the information in Solution box 10
            Case 9
                If Solution10.Items.Count > 0 Then
                    Dim Root11 =
                    (
                    From k In doc...<Section10>
                    Where k.@id = Solution10.Text
                    From l In k.<Root11>.<Section11>
                    Select l.@id
                    ).ToArray


                    Solution11.Items.Add(Root11)
                End If
                'Solution11.click event, fills Solution box 12 with values based on the information in Solution box 11
            Case 10
                If Solution11.Items.Count > 0 Then
                    Dim Root12 =
                    (
                    From l In doc...<Section11>
                    Where l.@id = Solution11.Text
                    From m In l.<Root12>.<Section12>
                    Select m.@id
                    ).ToArray


                    Solution12.Items.Add(Root12)
                End If


        End Select
    End Sub
 
Last edited:
Hi,

I looked at this yesterday and I still have not fully got my head round what you are doing but to answer your question on the list view. Here is how you can do it. Add a ListView to a form and set it's View property to Details. Add the code below and change the file name to your own and then run.

VB.NET:
Imports System.Xml
Imports System.IO
 
Public Class Form1
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim strmReader As New StreamReader("d:\temp\XMLFile2.xml", System.Text.Encoding.UTF8)
    Dim xmlReader As XmlTextReader = New XmlTextReader(strmReader)
    xmlreader.WhitespaceHandling = WhitespaceHandling.None
 
    Dim xmlDoc As New XmlDocument()
    xmlDoc.Load(xmlReader)
 
    Dim strXMLPos As String = "//Root/"
    For Counter As Integer = 1 To 12
      strXMLPos += "Root" & Counter & "/Section" & Counter
      Dim xml_nodelist As XmlNodeList = xmlDoc.SelectNodes(strXMLPos)
      For Each curNode As XmlNode In xml_nodelist
        Dim LVI As New ListViewItem
        With LVI
          .Text = curNode.Attributes.ItemOf("Name").InnerText
          .SubItems.Add(curNode.Attributes.ItemOf("id").InnerText)
          .SubItems.Add(curNode.Attributes.ItemOf("Label").InnerText)
        End With
        ListView1.Items.Add(LVI)
      Next
      strXMLPos += "/"
    Next
    AddListViewHeader()
  End Sub
 
  Private Sub AddListViewHeader()
    Dim Headers() As ColumnHeader = {New ColumnHeader With {.Text = "Name", .Width = 200},
                                     New ColumnHeader With {.Text = "ID", .Width = 90, .TextAlign = HorizontalAlignment.Center},
                                     New ColumnHeader With {.Text = "Label", .Width = 200}}
    ListView1.Columns.Clear()
    ListView1.Columns.AddRange(Headers)
  End Sub
End Class

I tried solving this using a Dataset, which worked fine, but I was not able to access the information contained in your attributes so I had to revert back to using for loops above.

Hope that helps.

Cheers,

Ian
 
Thanks Ian

Let me try explain myself a little better, this picture below shows an graphical interpretation of my xml file, each parent node has two child nodes and so on for twelve generations.

Screen Shot.jpg

I want to be able to display two of those nodes in a list <id>.<Name>.<Label>, in two rows. there will be twelve list views in total each will be dependent on the previous one and clicking or double clicking on your selection will take you to the next choice.

So
List View 1
Is it Red or Blue (if i click on this item then the next listview will display)
Is it Black or Green

List View 2
Is it Red
Is it Blue (if i click on this item then the next listview will display)

List View 3
Is it light Blue
Is it dark Blue (if i click on this item then the next listview will display)

List View 4
You have found dark blue

But if any of the list view selections are changed above then the remaining listviews should clear until the route is specified

VB.NET:
<?xml version="1.0" encoding="utf-8"?>
<Root>
    <Root1>
        <Section1 Name="Is it Red or Blue" id ="1" Label="Further Info">
            <Root2>
                <Section2 Name="Is it Blue" id ="2" Label="Further Info">
                    <Root3>
                        <Section3 Name="Is it dark Blue" id ="3" Label="Further Info">
                            <Root4>
                                <Section4 Name="You have found dark Blue" id ="4" Label="Further Info">
                                </Section4>
                            </Root4>
                        </Section3>    
                    </Root3>
                    <Root3>
                        <Section3 Name="Is it light Blue" id ="5" Label="Further Info">
                            <Root4>
                                <Section4 Name="You have found light blue" id ="6" Label="Further Info">
                                </Section4>
                            </Root4>
                        </Section3>    
                    </Root3>
                </Section2>
            </Root2>
            <Root2>
                <Section2 Name="Is it Red" id ="7" Label="Further Info">
                    <Root3>
                        <Section3 Name="Is it dark Red" id ="8" Label="Further Info">
                            <Root4>
                                <Section4 Name="You have found dark Red" id ="9" Label="Further Info">
                                </Section4>
                            </Root4>
                        </Section3>    
                    </Root3>
                    <Root3>
                        <Section3 Name="Is it light Red" id ="10" Label="Further Info">
                            <Root4>
                                <Section4 Name="You have found light Red" id ="11" Label="Further Info">
                                </Section4>
                            </Root4>
                        </Section3>    
                    </Root3>
                </Section2>
            </Root2>        
        </Section1>    
    </Root1>
    <Root1>
        <Section1 Name="Is it Green or Black" id ="12" Label="Further Info">
            <Root2>
                <Section2 Name="Is it Black" id ="13" Label="Further Info">
                    <Root3>
                        <Section3 Name="Is it dark Black" id ="14" Label="Further Info">
                            <Root4>
                                <Section4 Name="You have found dark Black" id ="15" Label="Further Info">
                                </Section4>
                            </Root4>
                        </Section3>    
                    </Root3>
                    <Root3>
                        <Section3 Name="Is it light Blue" id ="16" Label="Further Info">
                            <Root4>
                                <Section4 Name="You have found light black" id ="17" Label="Further Info">
                                </Section4>
                            </Root4>
                        </Section3>    
                    </Root3>
                </Section2>
            </Root2>
            <Root2>
                <Section2 Name="Is it Green" id ="18" Label="Further Info">
                    <Root3>
                        <Section3 Name="Is it dark Green" id ="19" Label="Further Info">
                            <Root4>
                                <Section4 Name="You have found dark Green" id ="20" Label="Further Info">
                                </Section4>
                            </Root4>
                        </Section3>    
                    </Root3>
                    <Root3>
                        <Section3 Name="Is it light Green" id ="21" Label="Further Info">
                            <Root4>
                                <Section4 Name="You have found light Green" id ="22" Label="Further Info">
                                </Section4>
                            </Root4>
                        </Section3>    
                    </Root3>
                </Section2>
            </Root2>        
        </Section1>    
    </Root1>
</Root>

I have tried your code (thank you very much) but i cant get it to display anything in the list
 
Hi,

That's makes more sense but not sure off the top of my head what is the best way to deal with this. Let me play with it for a while.

With regards my code, it is based on the XML structure you posted yesterday. Also make sure you have changed the View Property of the ListView to Details. If you have done all that you will need to step through the code to see what is different with the file you are using.

Hope that helps.

Cheers,

Ian
 
Just for simplicity i only need the id for reference in the xml because that is the only thing that will always be unique about the node, the text on some nodes could be identical and that causes the form to see nodes it shouldn't.

Also, i did consider using treeview to display the Name and a double click on the item would display the Label, if that makes things any easier, or more practical.
 
No Worries,

I am 20 mins away from posting a solution so I will go with what I have at the mo so that you can see how I am doing it then you can modify as you see fit. Back in half a hour.

Cheers,

Ian
 
Hi,

I have to go just now so I am posting what I have at the moment. Add 4 ListViews to a form, set their properties as follows:-
View=Detail
MultiSelect=False
FullRowSelect=True
HideSelection=False

Then add the code below, change the XML file name again and then run.

You will see that this is only half way there. The issue is that for any particular element that you select there are multiple nodes that are contained within that element and there is currently no way, that I have found so far, to identify which particular nodes needs to be displayed since there is no correlation between the parent and child nodes. You may need to add an attribute to the nodes to help you identify exactly which nodes need to be selected based on the parent that is being clicked on.

VB.NET:
Imports System.Xml
Imports System.IO
 
Public Class Form1
  Dim xmlDoc As New XmlDocument()
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim strmReader As New StreamReader("d:\temp\XMLFile2.xml", System.Text.Encoding.UTF8)
    Dim xmlReader As XmlTextReader = New XmlTextReader(strmReader)
 
    'read the file and load the first list view
    xmlReader.WhitespaceHandling = WhitespaceHandling.None
    xmlDoc.Load(xmlReader)
 
    Dim strXMLPos As String = "//Root/Root1/Section1"
    Dim xml_LV1List As XmlNodeList = xmlDoc.SelectNodes(strXMLPos)
    For Each curNode As XmlNode In xml_LV1List
      Dim LVI As New ListViewItem
      With LVI
        .Text = curNode.Attributes.ItemOf("Name").InnerText
        .SubItems.Add(curNode.Attributes.ItemOf("id").InnerText)
        .SubItems.Add(curNode.Attributes.ItemOf("Label").InnerText)
      End With
      ListView_1.Items.Add(LVI)
    Next
    'add each list view header
    AddListViewHeader(ListView_1)
    AddListViewHeader(ListView_2)
    AddListViewHeader(ListView_3)
    AddListViewHeader(ListView_4)
  End Sub
 
  'this add's the list view headers
  Private Sub AddListViewHeader(ByVal LV As ListView)
    Dim Headers() As ColumnHeader = {New ColumnHeader With {.Text = "Name", .Width = 200},
                                     New ColumnHeader With {.Text = "ID", .Width = 90, .TextAlign = HorizontalAlignment.Center},
                                     New ColumnHeader With {.Text = "Label", .Width = 200}}
    LV.Columns.Clear()
    LV.Columns.AddRange(Headers)
  End Sub
 
  'see that we handle all the listview SelectedIndexChanged events here
  Private Sub ListView_1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView_1.SelectedIndexChanged, ListView_2.SelectedIndexChanged, ListView_3.SelectedIndexChanged
    Const MaxListViews As Integer = 12
    Const ControlSelector As String = "ListView_"
 
    'cast the clicked list view to the listview type
    Dim myListView As ListView = DirectCast(sender, ListView)
 
    If myListView.SelectedItems.Count > 0 Then
      'identify and save the node location here
      Dim NodeLocation As Integer = CInt(myListView.Name.Substring(myListView.Name.LastIndexOf("_") + 1, (myListView.Name.Length - 1) - myListView.Name.LastIndexOf("_")))
 
      'clear the other list views
      For ctrlCount As Integer = NodeLocation + 1 To 4
        Dim ControlToClear As ListView = DirectCast(Me.Controls(ControlSelector & ctrlCount), ListView)
        ControlToClear.Items.Clear()
      Next
 
      If NodeLocation < MaxListViews Then
        'set an object to the next control to be updated
        Dim myNextControl As ListView = DirectCast(Me.Controls(ControlSelector & NodeLocation + 1), ListView)
        Dim strXMLPos As String = "//Root"
 
        'create the string to read nodes from the file
        For Counter As Integer = 1 To NodeLocation + 1
          strXMLPos += "/Root" & Counter & "/Section" & Counter
        Next
 
        'get the node list at the specified location - this is where the error is
        'since we are picking up all nodes which bel;ong to the strXMLPos element
        Dim xml_LV_List As XmlNodeList = xmlDoc.SelectNodes(strXMLPos)
        For Each curNode As XmlNode In xml_LV_List
          Dim LVI As New ListViewItem
          With LVI
            .Text = curNode.Attributes.ItemOf("Name").InnerText
            .SubItems.Add(curNode.Attributes.ItemOf("id").InnerText)
            .SubItems.Add(curNode.Attributes.ItemOf("Label").InnerText)
          End With
          myNextControl.Items.Add(LVI)
        Next
        AddListViewHeader(myNextControl)
      End If
    End If
  End Sub
End Class

Hope that helps.

Cheers.

Ian
 
Thanks Ian,

My plan was to display the child nodes based on the ID of the previous listview

The code below works in a listbox and displays the child node of the parent clicked in the previous listbox, it checks the preceding listbox for the id and displays only child nodes relevant to it.

Although this worked OK, it did not give me the option to display the id, Name and Label together, hence why i changed to ListView

VB.NET:
Imports System.Xml
Public Class Form1
    Private doc As New XDocument


    'Load XML file "Test1.xml"and put the Name tag in Solution1's List Box
    'Private Sub - for Solution 1 list box
    Private Sub form1_load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        doc = XDocument.Load(IO.Path.Combine(Application.StartupPath, "Test1.xml"))


        Dim Root =
            (
                From A In doc...<Section1>
                Select Section1 = A.@id
                Order By Section1
            ).ToArray
        Solution1.DataSource = Root


        AddHandler Solution1.Click, AddressOf Solution1_Click
        Solution1_Click(Nothing, Nothing)


    End Sub
    'Private Sub - for Solution 2 list box, select Name from the choices in Root 2 Section 2 of the xml, based on the
    'value selected in Section 1 of the xml, each Solution boxes value changes based on the previouse Solution boxes value
    Private Sub Solution1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


        Solution2.DataSource = Nothing


        Dim Root2 =
        (
            From a In doc...<Section1>
            Where a.@id = Solution1.Text
            From l In a.<Root2>.<Section2>
            Select l.@id
        ).ToArray


        Solution2.DataSource = Root2


    End Sub


    'Private Sub - for Solution 3 list box
    Private Sub Solution2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Solution2.Click


        Dim Root3 =
        (
            From a In doc...<Section1>
            Where a.@id = Solution1.Text
            From l In a.<Root2>.<Section2>
            Where l.@id = Solution2.Text
            From c In l...<Section3>
            Select c.@id
        ).ToArray


        Solution3.DataSource = Root3


    End Sub
    'Private Sub - for Solution 4 list box
    Private Sub Solution3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Solution3.Click
        Dim Root4 =
        (
            From a In doc...<Section2>
            Where a.@id = Solution2.Text
            From l In a.<Root3>.<Section3>
            Where l.@id = Solution3.Text
            From c In l...<Section4>
            Select c.@id
        ).ToArray


        Solution4.DataSource = Root4


    End Sub
    'Private Sub - for Solution 5 list box
    Private Sub Solution4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Solution4.Click
        Dim Root5 =
        (
            From f In doc...<Section3>
            Where f.@id = Solution3.Text
            From q In f.<Root4>.<Section4>
            Where q.@id = Solution4.Text
            From g In q...<Section5>
            Select g.@id
        ).ToArray


        Solution5.DataSource = Root5


    End Sub
    'Private Sub - for Solution 6 list box
    Private Sub Solution5_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Solution5.Click
        Dim Root6 =
        (
            From h In doc...<Section4>
            Where h.@id = Solution4.Text
            From w In h.<Root5>.<Section5>
            Where w.@id = Solution5.Text
            From j In w...<Section6>
            Select j.@id
        ).ToArray


        Solution6.DataSource = Root6


    End Sub
    'Private Sub - for Solution 7 list box
    Private Sub Solution6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Solution6.Click
        Dim Root7 =
        (
            From k In doc...<Section5>
            Where k.@id = Solution5.Text
            From r In k.<Root6>.<Section6>
            Where r.@id = Solution6.Text
            From m In r...<Section7>
            Select m.@id
        ).ToArray


        Solution7.DataSource = Root7


    End Sub
    'Private Sub - for Solution 8 list box
    Private Sub Solution7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Solution7.Click
        Dim Root8 =
        (
            From n In doc...<Section6>
            Where n.@id = Solution6.Text
            From t In n.<Root7>.<Section7>
            Where t.@id = Solution7.Text
            From o In t...<Section8>
            Select o.@id
        ).ToArray


        Solution8.DataSource = Root8


    End Sub

Thanks for all your time and effort so far.
 
Hi,

I have to laugh at myself. It did not even occur to me this morning to use LINQ to query the nodes to get the ones that I needed to display. Dooohhhh!

Anyway here is the solution. By the way, make sure you call each ListView based on the following structure, ListView_X. X being 1 to your total ListViews.

VB.NET:
Option Strict On
Imports System.Xml
Imports System.IO
 
Public Class Form1
  Dim xmlDoc As New XmlDocument()
 
  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim strmReader As New StreamReader("d:\temp\XMLFile2.xml", System.Text.Encoding.UTF8)
    Dim xmlReader As XmlTextReader = New XmlTextReader(strmReader)
 
    'read the file and load the first list view
    xmlReader.WhitespaceHandling = WhitespaceHandling.None
    xmlDoc.Load(xmlReader)
 
    Dim strXMLPos As String = "//Root/Root1/Section1"
    Dim xml_LV1List As XmlNodeList = xmlDoc.SelectNodes(strXMLPos)
    For Each curNode As XmlNode In xml_LV1List
      Dim LVI As New ListViewItem
      With LVI
        .Text = curNode.Attributes.ItemOf("Name").InnerText
        .SubItems.Add(curNode.Attributes.ItemOf("id").InnerText)
        .SubItems.Add(curNode.Attributes.ItemOf("Label").InnerText)
      End With
      ListView_1.Items.Add(LVI)
    Next
    'add each list view header
    AddListViewHeader(ListView_1)
    AddListViewHeader(ListView_2)
    AddListViewHeader(ListView_3)
    AddListViewHeader(ListView_4)
  End Sub
 
  'this add's the list view headers
  Private Sub AddListViewHeader(ByVal LV As ListView)
    Dim Headers() As ColumnHeader = {New ColumnHeader With {.Text = "Name", .Width = 200},
                                     New ColumnHeader With {.Text = "ID", .Width = 90, .TextAlign = HorizontalAlignment.Center},
                                     New ColumnHeader With {.Text = "Label", .Width = 200}}
    LV.Columns.Clear()
    LV.Columns.AddRange(Headers)
  End Sub
 
  'see that we handle all the listview SelectedIndexChanged events here
  Private Sub ListView_1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView_1.SelectedIndexChanged, ListView_2.SelectedIndexChanged, ListView_3.SelectedIndexChanged
    'cast the clicked list view to the listview type
    Dim myListView As ListView = DirectCast(sender, ListView)
 
    If myListView.SelectedItems.Count > 0 Then
      Const MaxListViews As Integer = 12
      Const ControlSelector As String = "ListView_"
 
      'identify and save the node location here
      Dim NodeLocation As Integer = CInt(myListView.Name.Substring(myListView.Name.LastIndexOf("_") + 1, (myListView.Name.Length - 1) - myListView.Name.LastIndexOf("_")))
 
      'clear the other list views
      For ctrlCount As Integer = NodeLocation + 1 To 4
        Dim ControlToClear As ListView = DirectCast(Me.Controls(ControlSelector & ctrlCount), ListView)
        ControlToClear.Items.Clear()
      Next
 
      If NodeLocation < MaxListViews Then
        'set an object to the next control to be updated
        Dim myNextControl As ListView = DirectCast(Me.Controls(ControlSelector & NodeLocation + 1), ListView)
        Dim strXMLPos As String = "//Root"
        'identify the selected ID of the element that has been clicked
        Dim SelectedID As Integer = CInt(myListView.SelectedItems(0).SubItems(1).Text)
 
        'create the string to read nodes from the file
        For Counter As Integer = 1 To NodeLocation + 1
          strXMLPos += "/Root" & Counter & "/Section" & Counter
        Next
 
        'get the node list at the specified location
        Dim nodesToDisplay = (From MyNode In xmlDoc.SelectNodes(strXMLPos) Select MyNode, NodeID = CInt(DirectCast(MyNode, XmlNode).ParentNode.ParentNode.Attributes.ItemOf("id").InnerText) Where NodeID = SelectedID)
        For Each curNode In nodesToDisplay
          Dim myNode As XmlNode = DirectCast(curNode.MyNode, XmlNode)
          Dim LVI As New ListViewItem
          With LVI
            .Text = myNode.Attributes.ItemOf("Name").InnerText
            .SubItems.Add(myNode.Attributes.ItemOf("id").InnerText)
            .SubItems.Add(myNode.Attributes.ItemOf("Label").InnerText)
          End With
          myNextControl.Items.Add(LVI)
        Next
        AddListViewHeader(myNextControl)
      End If
    End If
  End Sub
End Class

Hope that helps.

Cheers,

Ian
 
Thanks Ian,
Although i am not 100% sure what you mean about naming the listviews, if i simply rename them as:-

Listview_1
Listview_2
Listview_3 etc all the way to 12

I can get the first listview to load but that is as far as it goes

Roy
 
Hi,

What i have done is to rename my listviews as above and copied the code over, when i run it i get the Listview_1 to display my items and that is as far as it goes, so when i click nothing happens.

Thanks,

Roy
 
Hi,

OK, I think I know what has happened. Check the declaration of the Private Sub ListView_1_SelectedIndexChanged event and make sure it has the Handles clause for the three ListViews being:-

VB.NET:
Handles ListView_1.SelectedIndexChanged, ListView_2.SelectedIndexChanged, ListView_3.SelectedIndexChanged

The complete declaration should look like:-
VB.NET:
Private Sub ListView_1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView_1.SelectedIndexChanged, ListView_2.SelectedIndexChanged, ListView_3.SelectedIndexChanged

In addition, if you are adding your total 12 ListViews then do not forget to expand the Handles clause to include all but the last ListView since the last ListView should be the answer you are trying to display and therefore does not need to be handled in this way.

Hope that sorts it.

Cheers,

Ian
 

Latest posts

Back
Top