The tip-of-the-month shows how to dynamically add rows and columns to an Asp.Net Table. I created an Asp.Net, VB.Net project to demonstrate how to dynamically add rows and columns to the table. The sample creates as many as 20 rows and 6 columns. In this sample 12 rows with 4 columns were created. The sample output:
Click the 'Do Again' button to create another sample.
The Operative Code
Private Sub AddTableRow(ByVal iRow As Integer, ByVal oTable As Table)
Dim oRow As New TableRow
With oRow
.BorderWidth = 1
.ID = "Row" & iRow
End With
oTable.Controls.Add(oRow)
End SubPrivate Sub AddTableColumn(ByVal sRowID As String, ByVal iRow As Integer, ByVal iCol As Integer)
Dim oCol As New TableCell
With oCol
.Text = "Row " & Format(iRow, "00") & "; " & "Column " & Format(iCol, "00")
End With
Dim oRow As TableRow = Me.TableDynamic.FindControl(sRowID)
If Not oRow Is Nothing Then
oRow.Controls.Add(oCol)
End If
End Sub
Download VB.Net Code
Download - VS2005 VB.Net Zipped File Size Dynamic Table Project with a Solution File DynamicTableSLN.zip 23k
Updated 2009/04/11