Solved

Looping through records in dataview in vb.net

Posted on 2014-03-27
4
1,669 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 300 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 300 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

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

22 Experts available now in Live!

Get 1:1 Help Now