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

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

问题描述

我想从 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....

推荐答案

你可以试试改成这样:

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天全站免登陆