vb.net创建数据表?

时间:2011-03-08 23:05:54

标签: vb.net datatable

我正在使用vb.net / winforms。 如何将包含三列的10行转换为DataSet / DataTable?

行是这样的:

Item-1, $100, 44

Item-2, $42, 3

etc

2 个答案:

答案 0 :(得分:17)

Dim Table1 As DataTable
Table1 = New DataTable("TableName")

Dim column1 As DataColumn = New DataColumn("Column1")
column1.DataType = System.Type.GetType("System.String")

Dim column2 As DataColumn = New DataColumn("Column2")
column2.DataType = System.Type.GetType("System.Int32") 
Dim column3 As DataColumn = New DataColumn("Column2")
column3.DataType = System.Type.GetType("System.Int32")

Table1.Columns.Add(column1)
Table1.Columns.Add(column2)
Table1.Columns.Add(column3)


Dim Row1 As DataRow
Row1 = Table1.NewRow()

Row1.Items("Column1") = "Item1"
Row1.Items("Column2") = 44
Row1.Items("Column3") = 99

Table1.Rows.Add(Row1)

' Repeat for other rows

答案 1 :(得分:14)

我知道这篇文章很老,但我相信这可以用更少的代码来解决。

' Declare DataTable
Dim Table1 As DataTable
' Define columns
Table1.Columns.Add("Column1", GetType(System.String))
Table1.Columns.Add("Column2", GetType(System.Int32))
Table1.Columns.Add("Column3", GetType(System.Int32))
' Add a row of data
Table1.Rows.Add("Item1", 44, 99)
Table1.Rows.Add("Item2", 42, 3)

来源DotNetPerls.com : VB.NET DataTable