Solved

Looping through records in dataview in vb.net

Posted on 2014-03-27
4
1,798 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

752 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