Solved

VB 2010:  Data from xml... How do I query the data?

Posted on 2010-09-12
13
613 Views
Last Modified: 2012-08-14
My application uses a single (large) xml file as the source for many different reports.  My on_load sub reads the xml into a dataset, and sets a datagridview.datasource to the dataset.

Here's a very simple table in order to explain my question.  Let's say that this is what my datagridview looks like:

Name  Type  Addr
fred      A         123
wilma  B         456
barney C         789

The user then has many different options utilizing the data.  For example:  I need to find all of Type:B, then do something with Name and Addr.

So here are my questions:
1.) Should I query the dataset, or the datagridview, or is there a better approach?
2.) Depending on answer to #1... How?

Can sql query syntax be used somehow?  I know how to run through all the rows in the datagridview, and this is how I've been doing it (but think there must be a better approach):

for r=0 to datagridview1.rowcount-1
  if datagridview.item(1,r).value="B" then
    <do_something>
  end if
next
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        DataSet1.ReadXml("c:\Test.xml")
        DataGridView1.DataSource = DataSet1
End Sub

Open in new window

0
Comment
Question by:motokent
  • 6
  • 5
  • 2
13 Comments
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33656669
try this
For Each row As DataGridViewRow In Me.DataGridView1.Rows
            If Not row.IsNewRow Then
                If row.Cells("Type").FormattedValue = "B" Then
                    do what you want
                End If
            End If
        Next

Open in new window

0
 
LVL 18

Assisted Solution

by:John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou earned 200 total points
ID: 33656688
Or for example
For Each row As DataGridViewRow In Me.DataGridView2.Rows
            If Not row.IsNewRow Then
                If row.Cells("Type").FormattedValue = "B" Then
                    row.Cells("Addr").Style.BackColor = Color.Azure
                End If
            End If
        Next

Open in new window

0
 

Author Comment

by:motokent
ID: 33656709
This is good, but what I'd like to know... is the datagridview approach (your suggestions, and the way I've been doing it) the best appproach?  I've seen things like LINQ to XML where I might be able to avoid datagridview totally, but not sure if there are any benefits to this (such as speed, or simplicity).
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33656720
there is no problem about the performance ...i am using this kind of approach many times,without problem ...
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33656841
Hi motokent;

In answer to your questions:
1.) Should I query the dataset, or the datagridview, or is there a better approach?

    Seeming you are using Visual Studio 2010 I would use Linq to DataSet to do what
    you need.
   
2.) Depending on answer to #1... How?

    See code snippet below.
   
Can sql query syntax be used somehow?  

    Linq has a form that has a syntax of SQL like, see snippet.

Fernando
' This query assumes that the DataTable in question is in the 
' DataSet table zero. Query's the DataTable for all record with
' Field name Type equal to B
Dim queryTypes = From t In ds.Tables(0).AsEnumerable()
                 Where t.Field(Of String)("Type") = "B"
                 Select New With {
                     .Name = t.Field(Of String)("Name"),
                     .Type = t.Field(Of String)("Type"),
                     .Addr = t.Field(Of Int32)("Addr")
                 }

For Each t In queryTypes
    ' Process all Type B records here.
    Console.WriteLine("Name {0}  :  Type {1}  :  Addr {2}", t.Name, t.Type, t.Addr.ToString())
Next

Open in new window

0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 300 total points
ID: 33656849
Hi motokent;

If you do not need the DataGridView, DataSet and DataTable you can directly query the XML with Linq to XML.

Fernando
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33656985
Respect !!:)
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33657017
@jtoutou, don't understand your last post?
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33657064
@Fernando  i just like and agree with  your comments...
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33657073
@jtoutou, OK now I understand and thank you.  ;=)
0
 

Author Closing Comment

by:motokent
ID: 33657587
Thanks to both of you.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 33657605
Not a problem, glad I was able to help.  ;=)
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33657610
very very welcome!!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question