vb.net loop through datarow and add each value into a listbox
12,416
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
Author by
Gbolahan
Updated on June 04, 2022Comments
-
Gbolahan almost 2 years
Private Function ColumnEqual(ByVal A As Object, ByVal B As Object) As Boolean If A Is DBNull.Value AndAlso B Is DBNull.Value Then Return True End If If A Is DBNull.Value OrElse B Is DBNull.Value Then Return False End If Return A.Equals(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 theDataRow
to the list.Any help?