Help with loading partial data in Grid using VB.NET

Hi,

How do you load a Data Gridview with partial data element from an xmll file using VB.NET. For example if my xml file contains 8 data elements, I only want to laod the grid with data element 1 and data elements 4 to 6.

Thanks,

Victor
vcharlesAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Hi Victor;

This will do what you asked for.
'' Create a List of Node names you wish to get the values for 
'' this case are Items1, Item4, and Item6
Dim getItems As List(Of String) = New List(Of String) From {"Item1", "Item4", "Item6"}
'' Open the XML document                                                                                      
Dim xdoc As XDocument = XDocument.Load("C:\Working Directory\test.xml")               
'' Get the nodes needed                                                                                      
Dim nodeValues As List(Of XmlNodeValue) = _                                           
    (From n In xdoc.Descendants() _                                                   
     Where getItems.Contains(n.Name.ToString()) _                                     
     Select New XmlNodeValue With _                                                   
            { _                                                                       
                .ItemValue = n.Value.ToString() _                                     
            } _                                                                       
    ).ToList()                                                                        
'' Place them in the DataGridView                                                                                      
DataGridView1.DataSource = nodeValues         


'' Because the values being assigned to a DataGridView
'' you need to make each value in a single column a property
Public Class XmlNodeValue                                                      
    Public Property ItemValue As String
End Class                              

Open in new window

0
 
Fernando SotoRetiredCommented:
Please give examples.
0
 
vcharlesAuthor Commented:
Hi Fernando,

For example if my xml file contains

<Root>
<table1>
<ID></ID>
<Item1><Item1>
<Item2><Item2>
<Item3><Item3>
<Item4><Item4>
<Item5><Item5>
<Item6><Item6>
<Item7><Item7>
<Item8><Item8>
<Item9><Item9>
</table1>
</Root>

How do I load the DataFridView with data from ID and from Item3 to Item6 data?

Thanks,

Victor
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
vcharlesAuthor Commented:
Hi,
Thank you, will test it from the office tomorrow.

Victor
0
 
vcharlesAuthor Commented:
Hi Fernando,

How do you modify the code to include the column headers for each data element?

Thanks,

Victor
0
 
Fernando SotoRetiredCommented:
Hi Victor;

The public property in this class becomes the Header title for the column. So in the sample code below the column Header will be ItemValue. To make it display a different header change ItemValue to what you want the header to be. Don't forget to change that property in the query select statement as well.

Public Class XmlNodeValue                                                      
    Public Property ItemValue As String
End Class        

Open in new window

0
 
vcharlesAuthor Commented:
Hi Fernando,

I notice one column Header (ItemValue) for all the data elements chosen, is there a way to have a column Header for each data element?

Thanks,

Victor
0
 
Fernando SotoRetiredCommented:
And what would you call each of the header elements?
0
 
vcharlesAuthor Commented:
Hi,

I would like to automatically named them the names of the data elements (i.e Item1, Item4, Item6).

Thanks,

Victor
0
 
Fernando SotoRetiredCommented:
Hi Victor;

Then you will need to do it manually because there is no standard function defined in the .Net to do that. The following should that that.
Dim getItems As List(Of String) = New List(Of String) From {"Item1", "Item4", "Item6"}

Dim xdoc As XDocument = XDocument.Load("C:\Working Directory\test1.xml")

Dim nodeValues As List(Of XmlNodeValue) = _
    (From n In xdoc.Descendants() _
     Where getItems.Contains(n.Name.ToString()) _
     Select New XmlNodeValue With _
            { _
                .Name = n.Name.ToString(), _
                .Value = n.Value _
            } _
    ).ToList()

'' Crate a DataTable object to fill
Dim table As DataTable = New DataTable()
'' Create all the column names to be used
For Each n As XmlNodeValue In nodeValues
    Dim col As New DataColumn(n.Name, GetType(String))
    table.Columns.Add(col)
Next
'' Fill the columns with the data
Dim newRow As DataRow = table.NewRow()
For i As Integer = 0 To newRow.ItemArray.Length - 1
    newRow(i) = nodeValues(i).Value
Next
'' Add the row to the DataTable
table.Rows.Add(newRow)
'' Bind the table to the DataGridView
DataGridView1.DataSource = table

Open in new window

0
 
vcharlesAuthor Commented:
Hi Fernando

I will try it and get back to you over the weekend.
Thank you.
V.
0
 
Fernando SotoRetiredCommented:
Hi Victory;

This question has been opened for a while, please assign points so that it will auto close.

Thanks
0
 
vcharlesAuthor Commented:
Thank You.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.