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

x
?
Solved

Looping through records in dataview in vb.net

Posted on 2014-03-27
4
Medium Priority
?
2,017 Views
Last Modified: 2014-03-27
Hi!

How do I write the loop so I dont manually have to do like this to achieve my result:( There can be 10 rows or 5 rows). I guess it must be something like:



  Dim id As String
        id = CType(sdsView.Table.Rows(0).ItemArray(5), String)
        id = id + CType(sdsView.Table.Rows(1).ItemArray(5), String)
        Label1.Text = id

Open in new window


For Each rowView As DataRowView in sdsView
    Dim row As DataRow = rowView.Row
    ' Do something '
Next

but I cant figure out exactly how



More code-example

Partial Class _13_mobileservice_mobileservice_group
    Inherits System.Web.UI.Page
    Private sdsView As DataView
    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        sdsView = (CType(SqlDataSource3.Select(DataSourceSelectArguments.Empty), DataView))
        Dim id As String
        id = CType(sdsView.Table.Rows(0).ItemArray(5), String)
        id = id + CType(sdsView.Table.Rows(1).ItemArray(5), String)
        Label1.Text = id
    End Sub

Open in new window

0
Comment
Question by:marcgu
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
jayakrishnabh earned 1200 total points
ID: 39958306
Dim dv As New DataView(New DataTable())
Dim id As String = String.Empty
For Each item As DataRow In dv.Table.Rows
      id = id & Convert.ToString(item.ItemArray)
Next
Label1.Text = id
0
 

Author Comment

by:marcgu
ID: 39958535
Hi!

Thanks for fast reply, but the problem is that it now only writs:

System.Object[] (for each record)     instead of the value(s) in the cell.

I probably made a misstake when I tried to use your code in my example.

I changed to this:

Partial Class _13_mobileservice_mobileservice_group
    Inherits System.Web.UI.Page
    'Private sdsView As DataView

   
 Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim dv As New DataView(New DataTable())
        dv = (CType(SqlDataSource3.Select(DataSourceSelectArguments.Empty), DataView))
        Dim id As String = String.Empty
        'id = CType(sdsView.Table.Rows(0).ItemArray(5), String)
        'id = id + CType(sdsView.Table.Rows(1).ItemArray(5), String)
        'Label1.Text = id



        'Dim id As String 
        For Each item As DataRow In dv.Table.Rows
            id = id & Convert.ToString(item.ItemArray)
        Next
        Label1.Text = id




    End Sub

Open in new window

0
 
LVL 5

Assisted Solution

by:jayakrishnabh
jayakrishnabh earned 1200 total points
ID: 39958612
replace item.ItemArray with item.ItemArray[5]
0
 

Author Closing Comment

by:marcgu
ID: 39958633
Thanks for quick and professional response. Now it works as I planned.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

926 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