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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.