cpt-crunchy
cpt-crunchy

Reputation: 391

Loop through the rows of single DataTable VB.Net

I'm having trouble trying to iterate a Datatable or even use the Linq class.

I have a table as Such:

(String)      (String)        (String)      (String)  (Integer)
Criteria    | description   |  Option   |   Title   | status
------------------------------------------------------------
Criteria 1    Lorem Ipsum      Option 1     Class #     0 
Criteria 1    Lorem Ipsum      Option 1     Class #     0 

Criteria 2    Lorem Ipsum      Option 1     Class #     0 
Criteria 2    Lorem Ipsum      Option 2     Class #     1 
Criteria 2    Lorem Ipsum      Option 3     Class #     0 

Criteria 3    Lorem Ipsum      Option 1     Class #     0 
Criteria 3    Lorem Ipsum      Option 2     Class #     1 
Criteria 3    Lorem Ipsum      Option 2     Class #     0 
Criteria 3    Lorem Ipsum      Option 3     Class #     0 

This is my CodeBehind

--VB.NET--
Dim dt As DataTable = StoredProcedure    

For Each row As DataRow In dt.Rows
  Dim sCriteriaOne As DataRow() = dt.Select("Criteria='Criteria 1'")

  Item.Append("<p>" + CStr(sCritOne.Item("Option")) + "</p>")
Next

Currently it won't compile and gives me 'Item' is not a member of DataRow()

What I'm trying to acheive is to be able to Loop through the DataTable and append that rows Item if it meets the condition. I had tried using Linq but my understanding on that is limited, though I did read through many other MSDN documents and SO posts on questions similar to this.

Upvotes: 1

Views: 5611

Answers (1)

Mary
Mary

Reputation: 15091

You got your loop started just fine but fell apart in the details. I guessed at what you were trying to do but maybe this will be enough to get you started. The $ before the string is an interpolated string which allows you to put variables in line in your string surrounded by { }. If you have an older version of vb you will have to use the old method String.Format. To check how the data is being updated check the immediate window after you end the program. That is what the Debug.Print is for.

 If row("Criteria").ToString = "Criteria 1" Then
                row("Criteria") = row("Criteria").ToString & $" <p>{CStr(row("Option"))}"
                Debug.Print(row("Criteria").ToString)
  End If

Upvotes: 1

Related Questions