This documentation is archived and is not being maintained.

HtmlTableRowCollection.Clear Method

Removes all HtmlTableRow objects from the HtmlTableRowCollection.

[Visual Basic]
Public Sub Clear()
[C#]
public void Clear();
[C++]
public: void Clear();
[JScript]
public function Clear();

Remarks

Use this method to remove all HtmlTableRow objects from the HtmlTableRowCollection and reset the Count property to 0.

Example

[Visual Basic, C#] The following example demonstrates how to use the Clear method to remove all items from the HtmlTableRowCollection. New entries are then added to the collection and displayed in the HtmlTable control.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>

<html>
<head>

   <script runat="server">

      Sub Button_Click(sender As Object, e As EventArgs) 
 
         Dim i As Integer
         Dim j As Integer
         Dim row As HtmlTableRow
         Dim cell As HtmlTableCell

         ' Clear the rows from the table.
         Table1.Rows.Clear()

         ' Create a new table.
         ' Iterate through the rows.
         For j=0 To 4 

            ' Create a new row and add it to the Rows collection.
            row = New HtmlTableRow()

            ' Provide a different background color for alternating rows.
            If (j Mod 2) = 1 Then
               row.BgColor = "Gainsboro"
            End If

            ' Iterate through the cells of a row.
            For i=0 To 4 
            
               ' Create a new cell and add it to the Cells collection.
               cell = New HtmlTableCell()
               cell.Controls.Add(new LiteralControl("row " & _ 
                                                 j.ToString() & _ 
                                                 ", cell " & _
                                                 i.ToString()))
               row.Cells.Add(cell)
            Next i
            Table1.Rows.Add(row)
           
         Next j
      
      End Sub

   </script>

</head>
<body>

   <form runat="server">

      <h3>HtmlTableRowCollection Example</h3>

      <table id="Table1" 
             Border="1" 
             BorderColor="black" 
             runat="server">

         <tr>
            <td>
               Cell 1
            </td>
            <td>
               Cell 2
            </td>
         </tr>
         <tr>
            <td>
               Cell 3
            </td>
            <td>
               Cell 4
            </td>
         </tr>

      </table>

      <br><br>
  
      <input type="button" 
             value="Create New Table"
             OnServerClick = "Button_Click" 
             runat="server"/>

   </form>

</body>
</html>

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

<html>
<head>

   <script runat="server">

      void Button_Click(Object sender, EventArgs e) 
      {
 
         // Clear the rows from the table.
         Table1.Rows.Clear();

         // Create a new table.
         // Iterate through the rows.
         for (int j=0; j<5; j++) 
         {

            // Create a new row and add it to the Rows collection.
            HtmlTableRow row = new HtmlTableRow();

            // Provide a different background color for alternating rows.
            if (j%2 == 1)
               row.BgColor="Gainsboro";

            // Iterate through the cells of a row.
            for (int i=0; i<5; i++) 
            {
               // Create a new cell and add it to the Cells collection.
               HtmlTableCell cell = new HtmlTableCell();
               cell.Controls.Add(new LiteralControl("row " + 
                                                 j.ToString() + 
                                                 ", cell " +
                                                 i.ToString()));
               row.Cells.Add(cell);
            }
            Table1.Rows.Add(row);
           
         }
      }

   </script>

</head>
<body>

   <form runat="server">

      <h3>HtmlTableRowCollection Example</h3>

      <table id="Table1" 
             Border="1" 
             BorderColor="black" 
             runat="server">

         <tr>
            <td>
               Cell 1
            </td>
            <td>
               Cell 2
            </td>
         </tr>
         <tr>
            <td>
               Cell 3
            </td>
            <td>
               Cell 4
            </td>
         </tr>

      </table>

      <br><br>
  
      <input type="button" 
             value="Create New Table"
             OnServerClick = "Button_Click" 
             runat="server"/>

   </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

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

HtmlTableRowCollection Class | HtmlTableRowCollection Members | System.Web.UI.HtmlControls Namespace | HtmlTableCell | HtmlTableRow | Cells | Count

Show: