Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-09-12
13
Medium Priority
?
622 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 800 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 64

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 64

Accepted Solution

by:
Fernando Soto earned 1200 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 64

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 64

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 64

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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

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 …
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 …
Integration Management Part 2
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month8 days, 4 hours left to enroll

824 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