从数据表中获取价值 [英] get value from DataTable

查看:70
本文介绍了从数据表中获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从DataTable中获取所有列值,并将其存储到ListBox中.这是我的代码

I want to get all column value from DataTable and store it to the ListBox. Here is my code

            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....

推荐答案

您可以尝试将其更改为此:

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.

这篇关于从数据表中获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆