Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am Struggling to attach or add new row in gridview after binding some existing bind data.

For Example,

I have 10 row in database.

When i am loading the page i should fetch all the 10 rows data. (Botton i will have "Add New Row Button")

In that, i want add 11th row when i click "Add New Row" button dynamically.

First columns contains Student Name ==> DropDown

Second Columns contains Stundent Number ==> TextBox.

So, when i click "Add new Row" button, My 11th Row should be Dropdown | Textbox to enter someting.

How to do this and attach the binding data...?

What I have tried:

VB
If ds.Tables(1).Rows.Count > 0 Then

                Dim dts As New DataSet

                'Dim tbl = New DataTable()
                'tbl.Columns.Add(New DataColumn("StudentName"))
                'tbl.Columns.Add(New DataColumn("StudentNumber"))
                'dts.Tables.Add(tbl)

                Dim dt As New DataTable()
                Dim dr As DataRow
                ' Add Columns to datatablse 
                dt.Columns.Add(New DataColumn("StudentName"))
                ''ColumnName1' represents name of datafield in grid 
                dt.Columns.Add(New DataColumn("StudentNumber"))
                ' Add empty row first to DataTable to show as first row in gridview 
                dr = dt.NewRow()
                dr("DocType") = ""
                dr("DocNumber") = ""

                dt.Rows.Add(dr)
                ' Get each row from gridview and add it to DataTable 
                For Each gvr As GridViewRow In grdDocList.Rows
                    dr = dt.NewRow()
                    dr("ColumnName1") = DirectCast(gvr.Cells(0).FindControl("Label"), Label).Text
                    dr("ColumnName2") = DirectCast(gvr.Cells(1).FindControl("Label"), Label).Text
                    dt.Rows.Add(dr)
                Next
                dts.Tables.Add(dt)

                dts = ds.Copy

                Me.grdList.DataSource = ds.Tables(1)
                Me.grdList.DataBind()

                ViewState("DocListTable") = dts.Tables(1)

                'After binding the gridview, we can then extract and fill the DropDownList with Data   
                Dim ddl1 As DropDownList = DirectCast(grdDocList.Rows(0).Cells(1).FindControl("StudentName"), DropDownList)
                FillDropDownList(ddl1)
end if
Posted
Updated 15-Mar-16 4:34am
v2
Comments
F-ES Sitecore 15-Mar-16 11:03am    
Google "asp.net gridview add row" and you'll find lots of solutions

http://www.codeproject.com/Articles/467788/Dynamically-adding-and-deleting-rows-from-ASP-NET

Easiest would probably be to have the "empty row" in the footer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900