首页 > 解决方案 > 在动态表 vb.net 中添加列表的所有元素

问题描述

Protected Sub createTable()
    Try

        Dim listB As New ArrayList()
        Dim dt As New DataTable
        Dim info As SessioneUtente = Session(SessionVariables.InformazioniUtente)
        Dim id As String = info.getIdDisciplinaDefault.ToString
        dt = operations.G_Number(id)

        If dt.Rows.Count = 0 Then
            notifica.Text = "There is no number for this id"

        Else

            For Each row In dt.Rows
                list.Add(row(0))
                listB.Add(row(0))
            Next
            listB.Add(23)
            listB.Add(24)
            listB.Add(25)

            Dim cell1 As New TableCell
            Dim cell2 As New TableCell
            Dim Nr As New Label
            Dim Icon As New Label
            Dim row1 As New TableRow
            Dim row2 As New TableRow
            For Each item In listB
                If list.Contains(item) Then    
                    Nr.Text = item
                   Icon.Text = "<img = src='../Images/Icon1.png' />"
                    cell1.Controls.Add(Nr)
                    cell2.Controls.Add(Icon)
                    row1.Cells.Add(cell1)
                    row2.Cells.Add(cell2)
                Else
                   Nr.Text = item & vbLf
                    Icon.Text = "<img = src='../Images/Icon2.png' />"
                    cell1.Controls.Add(Nr)
                    cell2.Controls.Add(Icon)
                    row1.Cells.Add(cell1)
                    row2.Cells.Add(cell2)
                End If

            Next
            tabel.Rows.Add(row1)
            tabel.Rows.Add(row2)
            notifica.Visible = False

        End If

    Catch ex As Exception
        Dim log As New Log
        log.writeLog("default", ex)
    End Try
End Sub   

我正在使用 arraylist 通过 VB.NET 动态填充表,但该表仅获取列表的最后一个元素。该列表从数据库中获取一些元素,其他元素是我自己添加的。我需要一些关于在动态表中显示列表的所有元素的帮助。先感谢您!

标签: vb.net

解决方案


我会尝试移动我在 foreach 循环内确定尺寸的变量。在我看来,您每次都在覆盖它们。您似乎在每个循环上都覆盖了相同的内存位置。将它们移动到循环内部应该为列表中的每个项目创建一个新的内存位置。我猜你看到的行数是正确的,但它们都具有相同的值,恰好是列表中的最后一项。这是因为它正在被覆盖,并且只有指向该内存位置的指针被添加到每个循环中。您还需要在每次迭代的循环内将其添加到表中的位置移动,因为现在您只添加设置行的最后一项,当您将这些变量移动到循环中时,它会抛出你一个错误。


推荐阅读