Click to See Complete Forum and Search --> : Gridview - add rows on the fly


tucson
February 6th, 2008, 05:41 PM
I created a gridview with a blank template fields row. Then after a user enters it and needs to add another row, there's a link button "Add." This instantiates the RowCommand event where I loop through the existing row and its entries, adds it to the gridview, then adds a new blank row.

No database involvement until the very end when they're ready to submit.

The problem is, when I loop through the existing entries and put it into the gridview, I can see the data. But when the gridview is displayed, the information entered in previous rows disappeared.

---------------------------------------------
Code to enter a dummy gridview row:
Dim dt As New DataTable
Dim dc As New DataColumn("description")
dtFiles.Columns.Add(dc)
Dim dr As DataRow = dtFiles.NewRow
dr("description") = "None"
dtFiles.Rows.Add(dr)
gFiles.DataSource = dtFiles
gFiles.DataBind()

-----------------------------------------------
Code that loops through existing rows before adding a blank row:
If dtFiles.Rows.Count = 0 Then
dtFiles.Columns.Add("tbDesc", GetType(TextBox))
dtFiles.Columns.Add("Add", GetType(LinkButton))
dtFiles.Columns.Add("Remove", GetType(LinkButton))
End If

Dim gr As GridViewRow
Dim nr As DataRow
For Each gr In gFiles.Rows
nr = dtFiles.NewRow
Dim tb As New TextBox
tb = gr.FindControl("tbDesc")
nr(1) = tb
dtFiles.Rows.Add(nr)
dsFiles.Tables.Add(dtFiles)
gFiles.DataSource = dsFiles
gFiles.DataBind()
Next

If e.CommandName = "Add" Then
'Add new row to dtFiles
nr = dtFiles.NewRow
dtFiles.Rows.Add(nr)
dsFiles.Tables.Add(dtFiles)
gFiles.DataSource = dsFiles
gFiles.DataBind()
gFiles.Visible = True
End If