1

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 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.