
Help adding Table Rows dynamically Please!
You need to store the # of rows and re-setup the table with the correct # of
rows each time when the button is pressed:
Here's a sample snipit using a DataSet as the storage mechanism - I then
bound it to a datagrid:
1) I created a blank webform and added a Button (Button1) and a DataGrid
(DataGrid1):
2) Add the following Code:
Public dt As New DataSet
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
' First time in I need to Create the DataSet with the correct #
of columns, and correct datatypes for each column.
dt = New DataSet
dt.Tables.Add("MyTable")
dt.Tables(0).Columns.Add("C1",
System.Type.GetType("System.String"))
Session("MYTABLE") = dt
Else
dt = Session("MYTABLE")
End If
' Bind the DataGrid to the table
Me.DataGrid1.DataSource = dt
DataGrid1.DataBind()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' Retrieve the DataSet
dt = Session("MYTABLE")
' Add a Row to the table.
Dim c(0) As String
c(0) = "Row # " & dt.Tables(0).Rows.Count + 1
dt.Tables(0).Rows.Add(c)
' Save the state & Re-Bind.
Session("MYTABLE") = dt
Me.DataGrid1.DataSource = dt
DataGrid1.DataBind()
End Sub
Thanks
GarySp & JonBraz
--
This posting is provided "AS IS" with no
warranties, and confers no rights.
Please do not send email directly to
this alias. This alias is for newsgroup
purposes only.
Quote:
> I need to add table rows dynamically that contain
> textboxes on a webform -- i have a button to "Add
> Sessions" and when the button is clicked it does add 1
> row, but when i go to add another, it either doesn't do
> anything or it just replaces the 1st added row --
> I guess my question is how would I continue to add table
> rows when the "add" button is clicked?
> Any help would be much appreciated!