这很容易。您所要做的就是将您的网格绑定到任何Colleciton,例如List、Array、DataTable。创建你的记录类
Public Class MyRecord
Private _name As String = ""
Private _grade As Integer = ""
Public Property Name() As String
Get
Return Me._name
End Get
Set(ByVal value As String)
Me._name = Value
End Set
End Property
Public Property Grade() As Integer
Get
Return Me._grade
End Get
Set(ByVal value As Integer)
Me._grade = Value
End Set
End Property
End Class
在您的 Page_Load 中将您的网格绑定到列表
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPotback Then
Dim list As New List(Of MyRecord)()
list.Add(New MyRecord() With { _
Key .Name = "dsfsdf", _
Key .Grade = 45 _
})
list.Add(New MyRecord() With { _
Key .Name = "dsfsd234f", _
Key .Grade = 50 _
})
Session("MyList") = list
GridView1.DataSource = list
GridView1.DataBind()
End If
End Sub
然后添加一些带有文本框和按钮的表单并处理按钮 on_click 事件
Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
Dim list As New List(Of MyRecord)
If Session("MyList") Is Not Nothing Then
list = DirectCast(Session("MyList"), List(Of MyRecord))
End If
list.Add(New MyRecord() With { _
Key .Name = txt1.Text, _
Key .Grade = Convert.ToInt32(txt2.Text) _
})
GridView1.DataSource = list
GridView1.DataBind()
End Sub
抱歉,如果有任何错误,我是 C# 开发人员