Solved

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

Posted on 2010-09-12
13
608 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

19 Experts available now in Live!

Get 1:1 Help Now