0

DTName 是DataTable在下面的代码中返回行...当我通过循环从 0 迭代它到它的计数时。仅显示第一行中的数据。

如何获取结果中显示的数据表的所有行?

DTName = GetClientNames()

   If Not DTName Is Nothing Then

      For i = 0 to DTName.Rows.count

         strName = DTName.Rows(i).Item("Client Name").Tostring()

      Next i

   End if
4

2 回答 2

3

For i = 0 to DTName.Rows.countIndexOutOfRangeException当值i等于时最终会抛出错误DTName.Rows.count,限制应该是DTName.Rows.count - 1

要从所有数据行中获取所有值,请将它们存储在List

    Dim strName As New List(Of String)

    For i = 0 to DTName.Rows.count - 1

       strName.Add(DTName.Rows(i)("Client Name").Tostring())

    Next i

或者,您可以Foreach像这样使用:

    For Each DR As DataRow In DTName.Rows

       strName.Add(DR("Client Name").Tostring())

    Next

我还建议您删除多余的检查,如果DTName.Rows.Count > 0

编辑:您可以声明strNamestring并将行值附加到它:

    For i = 0 to DTName.Rows.count - 1

        strName &= (DTName.Rows(i)("Client Name").Tostring() & ",")

    Next i

    Response.Write(strName)
于 2013-01-25T21:24:06.323 回答
0

加倍检查ColumnName列的实际值。

For Each item As DataRow In dataTable.Rows
    strName = item("Client Name").ToString()
    Console.WriteLine(strName)
Next
于 2013-01-25T22:03:10.867 回答