Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

TableCellCollection Class

Encapsulates a collection of TableHeaderCell and TableCell objects that make up a row in a Table control. This class cannot be inherited.

For a list of all members of this type, see TableCellCollection Members.

System.Object
   System.Web.UI.WebControls.TableCellCollection

[Visual Basic]
NotInheritable Public Class TableCellCollection
   Implements IList, ICollection, IEnumerable
[C#]
public sealed class TableCellCollection : IList, ICollection,
   IEnumerable
[C++]
public __gc __sealed class TableCellCollection : public IList,
   ICollection, IEnumerable
[JScript]
public class TableCellCollection implements IList, ICollection,
   IEnumerable

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

Use this class to programmatically manage a collection of TableCell objects that make up a row in a Table control. This class is commonly used to add or remove cells from a row in 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.

Example

[Visual Basic, C#] The following example demonstrates how to programmatically fill a Table control. TableCell objects, which represent individual cells, are added to TableRow objects, which represent the individual rows, through the Cells property.

[Visual Basic] 

<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
 <head>
 
    <script runat="server">

        Sub Page_Load(sender As Object, e As EventArgs)
            ' Generate rows and cells.           
            Dim numrows As Integer = 4
            Dim numcells As Integer = 6
            Dim counter As Integer = 1
            Dim j As Integer
            For j = 0 To numrows - 1
                Dim r As New TableRow()
                Dim i As Integer
                For i = 0 To numcells - 1
                    Dim c As New TableCell()
                    c.Text = counter.ToString()
                    counter += 1
                    r.Cells.Add(c)
                Next i
                Table1.Rows.Add(r)
            Next j
        End Sub

        Sub Button_Click_Coord(sender As Object, e As EventArgs)            
            Dim i As Integer
            For i = 0 To Table1.Rows.Count - 1
                Dim j As Integer
                For j = 0 To (Table1.Rows(i).Cells.Count) - 1                    
                    Table1.Rows(i).Cells(j).Text = "(" & j.ToString() & _
                        ", " & i.ToString() & ")"
                Next j
            Next i 
        End Sub

        Sub Button_Click_Number(sender As Object, e As EventArgs)
            Dim counter As Integer = 1
            
            Dim i As Integer
            For i = 0 To Table1.Rows.Count - 1
                Dim j As Integer
                For j = 0 To (Table1.Rows(i).Cells.Count) - 1                    
                    Table1.Rows(i).Cells(j).Text = counter.ToString()
                    counter += 1
                Next j 
            Next i
        End Sub
 
    </script>
 
 </head>
 
 <body>
 
    <h3>TableCellCollection Example</h3>
    <form runat="server">
       <asp:Table id="Table1" 
            runat="server"/>
       <br>
       <center>
          <asp:Button id="Button1"
               Text="Display Table Coordinates"
               OnClick="Button_Click_Coord"
               runat="server"/>
          <asp:Button id="Button2"
               Text="Display Cell Numbers"
               OnClick="Button_Click_Number"
               runat="server"/>
       </center>
 
    </form>
 
 </body>
 </html>
    

[C#] 

<%@ Page Language="C#" AutoEventWireup="True" %>

<html>
 <head>
 
    <script runat="server">
       void Page_Load(Object sender, EventArgs e) 
       {
          // Generate rows and cells.           
          int numrows = 4;
          int numcells = 6;
          int counter = 1;
          for (int j=0; j<numrows; j++) 
          {          
             TableRow r = new TableRow();
             for (int i=0; i<numcells; i++) 
             {
                TableCell c = new TableCell();
                c.Text=counter.ToString();
                counter++;
                r.Cells.Add(c);
             }
             Table1.Rows.Add(r);
          }
       }
 
       void Button_Click_Coord(object sender, EventArgs e) 
       {
          
          for (int i=0; i<Table1.Rows.Count; i++) 
          {          
             for (int j=0; j<Table1.Rows[i].Cells.Count; j++) 
             {
                
                Table1.Rows[i].Cells[j].Text = "(" + 
                   j.ToString() + ", " + i.ToString() + ")";
                
             }            
          }
 
       }
      
       void Button_Click_Number(object sender, EventArgs e) 
       {
 
          int counter = 1;
          
          for (int i=0; i<Table1.Rows.Count; i++) 
          {          
             for (int j=0; j<Table1.Rows[i].Cells.Count; j++) 
             {
                
                Table1.Rows[i].Cells[j].Text = counter.ToString();
                counter++;
                
             }            
          }
 
       }
 
    </script>
 
 </head>
 
 <body>
 
    <h3>TableCellCollection Example</h3>
    <form runat="server">
       <asp:Table id="Table1" 
            runat="server"/>
       <br>
       <center>
          <asp:Button id="Button1"
               Text="Display Table Coordinates"
               OnClick="Button_Click_Coord"
               runat="server"/>
          <asp:Button id="Button2"
               Text="Display Cell Numbers"
               OnClick="Button_Click_Number"
               runat="server"/>
       </center>
 
    </form>
 
 </body>
 </html>
    

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.UI.WebControls

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

TableCellCollection Members | System.Web.UI.WebControls Namespace | TableRow | Rows | TableCell | Cells | TableHeaderCell | Table | TableCellCollection

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.