如何将字符串数组导入到datagridview或Datatable?

时间:2019-06-21 02:02:11

标签: vb.net datagridview datatable dataset

在VB.net中 如何将数据数组导入到Datagridview或DataTable(或数据集)中?

最初,我想将数据从xlsm文件导入到Datagridview或Datatable中,尽管OleDbConnection似乎通过“ Provider = Microsoft.ACE.OLEDB.12.0; Data Source”限制为255列!我的数据大约有3000行和550列。

因此,我将方法更改为通过“ IO.File.ReadAllLines(path)”通过.csv文件进行处理,如下面的代码所示,尽管我确定有更快速的编码方法,请您帮忙导入数据数组到Datagridview或DataTable(或Dataset)?

    Dim lines = IO.File.ReadAllLines(path)
    Dim tbl = New DataTable
    Dim colCount = lines.First.Split(","c).Length

    Dim Novalinha As Integer
    Novalinha = 0

    For i As Int32 = 1 To colCount
        tbl.Columns.Add(New DataColumn("Column_" & i, GetType(String)))
    Next

    'For Each linha In lines
    '    Dim Items As String() = linha.Split(",")
    '    For i = 0 To colCount - 1
    '        tbl.Columns.Item(0).DefaultValue = Items(i)
    '    Next i
    '    tbl.Rows.Add(1)
    'Next

        DataGridView1.ColumnCount = colCount + 1
        DataGridView1.RowCount = lines.Length

        Novalinha = 0
        For Each linha In lines
            Dim Items As String() = linha.Split(",")
            For i = 0 To colCount - 1
                DataGridView1.Item(i, Novalinha).Value = Items(i)
            Next
            Novalinha += 1
        Next

我设法获取了所有数据的Datagridview,但是我无法获取如何使用数据表或数据集。

1 个答案:

答案 0 :(得分:0)

            @Override
            void visitInvokeDynamicInsn(String name1, String desc1, Handle bsm, Object... bsmArgs) {
                Handle handle2 = new Handle(6,"com/curious/autotrack/demo/MainActivity","testsss","(Ljava/lang/PrintStream;Landroid/view/View;)V")
                bsmArgs[1] = handle2
                super.visitInvokeDynamicInsn(name1, '(Ljava/lang/PrintStream;)Landroid/view/View$OnClickListener;', bsm, bsmArgs)
            }

然后将datagridview的数据源设置为tbl

For Each linha In lines
    Dim Items As String() = linha.Split(",")
    Dim newRow As DataRow = tbl.NewRow
    For i = 0 To colCount - 1
        newRow("Column_" & (i+1).ToString) = Items(i)
    Next i
    tbl.Rows.Add(newRow)
Next
tbl.AcceptChanges

删除这些行

DatagridView1.DataSource = tbl