vb.net loop through datarow and add each value into a listbox

0 votes
asked Jul 23, 2010 by gbolahan
      Private Function ColumnEqual(ByVal A As Object, ByVal B As Object) As Boolean

        If A Is DBNull.Value And B Is DBNull.Value Then
          Return True 
        End If

        If A Is DBNull.Value Or B Is DBNull.Value Then
          Return False 
        End If
        Return A = B
     End Function

       Public lastV As Object
        For Each dr In wData.Rows
             If lastV Is Nothing OrElse Not ColumnEqual(lastV, dr("table1")) Then
               ''check if first value is nothing
               If lastV = Nothing Then
                lastV = "00"
                 l = "0"
             dr("t1") = lastV
              dr("n1") = l
              End If
             ListBox1.Items.Add(lastV & " <--> " & l)
              lastV = dr("table1")
              l = 1
            ElseIf lastV Is Nothing OrElse ColumnEqual(lastV, dr("table1")) Then
              l += 1
              End If

i use this code to loop through my datarow. It adds each record into a listbox but it does not add the last record in the datarow to the list.

Any help.


1 Answer

0 votes
answered Jul 26, 2010 by logan-young

If I understand you correctly, you want to add each column in the datarow as a new ListBox item in your ListBox.

If I'm right, then the following example would do it. I'd use a DataSet:

Dim i As Integer = 0
For j As Integer = 0 To wData.Tables(0).Columns.Count - 1

    If j = wData.Tables(0).Column.Count - 1 Then
        i = i + 1
    End If

This will add the contents of each column in row i to the ListBox. The If statement stops it from incrementing i until it's added the last column.

Please bear in mind I haven't tested this code.


Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter