vb.net 从数据表中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8747004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get value from DataTable
提问by Flashidkz
I want to get all column value from DataTable and store it to the ListBox. Here is my code
我想从 DataTable 获取所有列值并将其存储到 ListBox。这是我的代码
If myTableData.Rows.Count > 0 Then
For i As Integer = 0 To myTableData.Rows.Count
Dim DataType() As String = myTableData.Rows(i).Item(1)
ListBox2.Items.AddRange(DataType)
Next
End If
but when I compile that code, I got error message like this :
但是当我编译该代码时,我收到如下错误消息:
Unable to cast object of type 'System.String' to type 'System.String[]'
so, how to resolve this problem?? Please help me....
那么,如何解决这个问题??请帮我....
回答by LarsTech
You can try changing it to this:
您可以尝试将其更改为:
If myTableData.Rows.Count > 0 Then
For i As Integer = 0 To myTableData.Rows.Count - 1
''Dim DataType() As String = myTableData.Rows(i).Item(1)
ListBox2.Items.Add(myTableData.Rows(i)(1))
Next
End If
Note: Your loop needs to be one less than the row count since it's a zero-based index.
注意:您的循环需要比行数少 1,因为它是一个从零开始的索引。
回答by Richard
It looks like you have accidentally declared DataType
as an array rather than as a string.
看起来您不小心将其声明DataType
为数组而不是字符串。
Change line 3 to:
将第 3 行更改为:
Dim DataType As String = myTableData.Rows(i).Item(1)
That should work.
那应该工作。