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"
              Else
             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
        Next

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.

Thanks

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
    ListBox1.BeginUpdate()
    ListBox1.Items.Add(wData.Tables(0).Rows(i)(j).ToString)
    ListBox1.EndUpdate

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

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.

HTH

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

...