1

I am wondering how to add rows to an already populated datatable with a button. the code I have so far is this...

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dt As New DataTable("Dt")
        dt.Columns.Add("test1")
        dt.Columns.Add("test2")
        dt.Columns.Add("test3")

        GridView1.DataSource = dt
        GridView1.DataBind()

    End Sub

    Protected Sub btnadd_Click(sender As Object, e As EventArgs) Handles btnadd.Click

        Dim dt As New DataTable

        dt.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text)
        GridView1.DataSource = dt
        GridView1.DataBind()

    End Sub

1 Answer 1

1

it works for me. Because am I storing the DataTable in ViewState so that I can reference the current data associated within the DataTable when it postback. You are initializing DataTable in Page_Load method including header of DataTable. When you are clicking on btnadd button then you are not getting existing DataSource. If you assign dt in ViewState("DataSource ") in Page_Load method and declare again DataTable in btnAdd_Click method like Dim dt As DataTable = DirectCast(ViewState("DataSource "), DataTable) then it will work because previous DataSource you are getting now. Finally I want to say that DataSource doesn't remember the DataSource you set in a previous rendering of the page.

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

            Dim dt As New DataTable("Dt")
            dt.Columns.Add("test1")
            dt.Columns.Add("test2")
            dt.Columns.Add("test3")
            GridView1.DataSource = dt
            GridView1.DataBind()
            ViewState("DataSource ") = dt

        End Sub

        Protected Sub btnadd_Click(sender As Object, e As EventArgs) Handles btnadd.Click

            Dim dt As DataTable = DirectCast(ViewState("DataSource "), DataTable)
            GridView1.DataSource = dt
            GridView1.DataBind()
            dt.Rows.Add("Hi1", "Hi2", "Hi2")
            GridView1.DataSource = dt
            GridView1.DataBind()

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

5 Comments

I will upvote this as a good answer if you add an explanation of how and why this works. Simply saying it works for me. doesn't give the OP any information as to why it works, what they missed out and what they should do. And, also fixup your formatting
This works, but I am only able to add one row at a time. Is there a way to continually add more rows?
Nevermind, I got it to work, just had to change a few things in the page loadout. thanks for the help guys.
@JayV Thanks for your suggestion. I have added little description. Hope that would be helpful.
@bman I am pleased that you were able to solve your all problems.

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.