如何将数组写入数据表中的不同列?

时间:2019-04-15 21:05:21

标签: arrays vb.net datatable

我需要从一个数组生成一个数据表,并且需要将该数组添加到该数据表的不同列中。 VB.net代码类似于: DataTable.Rows.Add(ID,(A(I),I = 1至100),但是我知道这是行不通的。有人可以告诉我该怎么做,所以我不必从A(1 )到A(100),例如DataTable.Rows.Add(ID,A(1),A(2),.....,A(100))?先谢谢您!

1 个答案:

答案 0 :(得分:0)

要做这样的事情。
    将table1设为新DataTable(“ Items”)

 ' Add columns
 Dim idColumn As New DataColumn("id", GetType(System.Int32))
 table1.Columns.Add(idColumn)
 For i As Integer = 1 To 100
   Dim itemColumn As New DataColumn("item" & i.ToString, GetType(System.Int32))
   table1.Columns.Add(itemColumn)
Next i

 ' Set the primary key column.
 table1.PrimaryKey = New DataColumn() {idColumn}

 ' Add RowChanged event handler for the table.
 AddHandler table1.RowChanged, AddressOf Row_Changed

' Add some rows.
 Dim row As DataRow
   row = table1.NewRow()
   row("id") = 1
 For i As Integer = 1 To 100
   row("item" + i.toString) = a(i)
 Next i
 table1.Rows.Add(row)
 ' Accept changes.
 table1.AcceptChanges()