在gridview中插入多行
我有以下代码将新行添加到数据表中,然后将其绑定到网格视图。 每当我单击 Button2 时,我都需要添加一个新行。
我需要在代码中更改什么,以便在将它们提交到数据库之前可以拥有多行?
私有子 BindGrid() Dim DT 作为新数据表 暗淡行作为数据行
DT.Columns.Add(New System.Data.DataColumn("Nome"))
DT.Columns.Add(New System.Data.DataColumn("Morada"))
Row = DT.NewRow
Row(0) = Nome.Text
Row(1) = Morada.Text
DT.Rows.Add(Row)
Dados.DataSource = DT
Dados.DataBind()
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
BindGrid()
End Sub
I have the following code to add a new row into a datatable and then bind it to a gridview.
I need to add a new row anytime i click the Button2.
What do i need to change in the code so i can have multiple rows before i submit them to a database?
Private Sub BindGrid()
Dim DT As New DataTable
Dim Row As DataRow
DT.Columns.Add(New System.Data.DataColumn("Nome"))
DT.Columns.Add(New System.Data.DataColumn("Morada"))
Row = DT.NewRow
Row(0) = Nome.Text
Row(1) = Morada.Text
DT.Rows.Add(Row)
Dados.DataSource = DT
Dados.DataBind()
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
BindGrid()
End Sub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您单击该按钮时,会发生回发并且页面再次从头开始加载。发生这种情况时,
Page_Load
再次执行,然后Button2_Click
运行。如果我假设用户输入一些文本以添加到 GridView,那么您将在
Button2_Click
中读取此文本。然后您可以将其添加到 GridView,然后您需要再次调用DataBind
。When you click on the button, a post-back occurs and the page loads from scratch again. When this happens,
Page_Load
executes again and thenButton2_Click
runs.If I may presume that the user enters some text to add to the GridView, then you'll read this text in
Button2_Click
. You can then add it to the GridView and then you'll need to callDataBind
again.