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
答案 0 :(得分:3)
For i = 0 to DTName.Rows.count
的值等于IndexOutOfRangeException
时, 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
编辑:您可以将strName
声明为string
并向其追加行值:
For i = 0 to DTName.Rows.count - 1
strName &= (DTName.Rows(i)("Client Name").Tostring() & ",")
Next i
Response.Write(strName)
答案 1 :(得分:0)
双击以检查列的实际ColumnName
。
For Each item As DataRow In dataTable.Rows
strName = item("Client Name").ToString()
Console.WriteLine(strName)
Next