Solved

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

Posted on 2010-09-12
13
612 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now