System.Web.UI.WebControls Namespace Table Class
Returns the collection of rows in a Table.
Script |
[ TableRowCollection variable = ] Table.Rows |
This property can only be used programmatically; it cannot be set when declaring the control.
The property is read/write with no default value.
Use the Rows collection to programmatically access the rows in a given Table control. Using this property, you can dynamically add rows to, remove rows from, and loop thru the rows in, the collection.
If no <asp:tablerow> elements are defined for the table, an empty TableRowCollection object is returned.
The following example shows use of the Rows collection to dynamically add a specified number of rows to a Table control at run time, based on user input.
// loop through the given number of rows
for ( int r = 0; r < numrows; r++ ) {
// instantiate a new row
TableRow row = new TableRow ( );
// set backcolor for alternating rows
if ( r%2 == 1 ) {
row.BackColor = System.Drawing.Color.Beige;
}
// loop through the given number of cells
for ( int c = 0; c < numcells; c++ ) {
// instantiate a new row
TableCell cell = new TableCell ( );
// add cell content
cell.Controls.Add ( new LiteralControl ( "row " +
r.ToString ( ) + ", cell " + c.ToString ( ) ) );
// add the cell to the Cells collection
row.Cells.Add ( cell );
}
// add the row to the Rows collection
myTable.Rows.Add ( row );
}
' loop through the given number of rows
Dim r As Integer
For r = 0 To numrows - 1
' instantiate a new row
Dim row As New TableRow ( )
' set backcolor for alternating rows
If r Mod 2 = 1 Then
row.BackColor = System.Drawing.Color.Beige
End If
' loop through the given number of cells
Dim c As Integer
For c = 0 To numcells - 1
' instantiate a new row
Dim cell As New TableCell ( )
' add cell content
cell.Controls.Add ( New LiteralControl ( "row " + _
r.ToString ( ) + ", cell " + c.ToString ( ) ) )
' add the cell to the Cells collection
row.Cells.Add ( cell )
Next c
' add the row to the Rows collection
myTable.Rows.Add ( row )
Next r |
|
C# |
VB |
Show me
Table Members Cells