vb.net 循环遍历数据行并将每个值添加到列表框中

发布于 2024-09-11 07:09:29 字数 923 浏览 8 评论 0原文

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

我使用此代码循环访问我的 DataRow

它将每条记录添加到 ListBox 中,但不会添加列表中的最后一条记录 DataRow 添加到列表中。

有什么帮助吗?

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 the
DataRow to the list.

Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

£噩梦荏苒 2024-09-18 07:09:29

如果我理解正确,您希望将数据行中的每一列添加为列表框中的新列表框项。

如果我是对的,那么下面的例子就可以做到。我将使用数据集:

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

这会将第 i 行中每列的内容添加到列表框中。 If 语句会阻止它增加 i,直到添加最后一列。

请记住我还没有测试过这段代码。

华泰

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文