views:

35

answers:

1

Hello. 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
+1  A: 

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 then Button2_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 call DataBind again.

Rice Flour Cookies