动态生成DataTable
Imports System
Imports System.Data
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Partial Class _Default
Inherits System.Web.UI.Page
Protected WithEvents myDataTable As DataTable
Protected WithEvents produces As DataGrid
Protected WithEvents eventsList As Label
Protected Sub GridView1_SelectedIndexChanged()
myDataTable = CType(Cache.Get("myDataTable"), DataTable)
'if myDataTable is not in the Cache,create it
If myDataTable Is Nothing Then
myDataTable = New DataTable("Produces")
'Build the produce schema
myDataTable.Columns.Add("ID", Type.GetType("System.Int32"))
myDataTable.Columns.Add("Name", Type.GetType("System.String"))
myDataTable.Columns.Add("Category", Type.GetType("System.Int32"))
'set up the ID column as the PrimaryKey
Dim pk(1) As DataColumn
pk(0) = myDataTable.Columns("ID")
myDataTable.PrimaryKey = pk
myDataTable.Columns("ID").AutoIncrement = True
myDataTable.Columns("ID").AutoIncrementSeed = 1
myDataTable.Columns("ID").ReadOnly = True
Dim tempRow As DataRow
Dim i As Int32 = 0
For i = 1 To 10
tempRow = myDataTable.NewRow()
If Math.IEEERemainder(i, 2) = 0 Then
tempRow("Name") = "jjj" & i.ToString
tempRow("Category") = 1
Else
tempRow("Name") = "dddd" & i.ToString
tempRow("Category") = 2
End If
myDataTable.Rows.Add(tempRow)
Next i
Cache.Insert("myDataTable", myDataTable)
End If
End Sub
Private Sub BindData()
GridView1_SelectedIndexChanged()
GridView1.DataSource = myDataTable
GridView1.DataBind()
End Sub
End Class
*************************
Xiang Xiang
QQ:86527965
*************************