TableRowCollection Class
Assembly: System.Web (in system.web.dll)
Use this class to programmatically manage a collection of TableRow objects. This class is commonly used to add or remove rows from a Table control.
Note: |
|---|
| A Table control contains a Rows collection that represents a collection of TableRow objects. Each TableRow represents an individual row in the table and contains a Cells collection that represents a collection of TableCell objects. These TableCell objects represent the individual cells in the table. To get an individual cell, you must first get a TableRow from the Rows collection of a Table control. You can then get a TableCell from the Cells collection of the TableRow. |
The following example demonstrates how to programmatically add rows to a table by adding TableRow objects, which represent the rows of the table, to the Table control through the Rows property.
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> void Page_Load(Object sender, EventArgs e) { // Generate rows and cells. int numRows = 3; int numCells = 2; for (int rowNum = 0; rowNum < numRows; rowNum++) { TableRow rw = new TableRow(); for (int cellNum = 0; cellNum < numCells; cellNum++) { TableCell cel = new TableCell(); cel.Text = String.Format( "row {0}, cell {1}", rowNum, cellNum); rw.Cells.Add(cel); } Table1.Rows.Add(rw); Table1.GridLines = GridLines.Both; Table1.CellPadding = 4; Table1.CellSpacing = 0; } } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Programmatic Table</title> </head> <body> <form id="form1" runat="server"> <div> <h3>Table Example, constructed programmatically</h3> <asp:Table id="Table1" runat="server"/> </div> </form> </body> </html>
<html>
<head>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e) {
// Generate rows and cells
int numrows = 3;
int numcells = 2;
for (int j=0; j<numrows; j++) {
TableRow r = new TableRow();
for (int i=0; i<numcells; i++) {
TableCell c = new TableCell();
c.Text="row " + j.ToString() + ", cell " + i.ToString();
r.Cells.Add(c);
}
Table1.Rows.Add(r);
}
}
</script>
</head>
<body>
<h3><font face="Verdana">Table Example, constructed programmatically</font></h3>
<form runat=server>
<asp:Table id="Table1"
runat="server"/>
</form>
</body>
</html>
- AspNetHostingPermission For operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.
Note: