HtmlTableRowCollection.Clear Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Removes all HtmlTableRow objects from the HtmlTableRowCollection collection.

Namespace:   System.Web.UI.HtmlControls
Assembly:  System.Web (in System.Web.dll)

public void Clear()

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

The following code example demonstrates how to use the Clear method to remove all items from the HtmlTableRowCollection collection. New entries are then added to the collection and displayed in the HtmlTable control.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  void Button_Click(Object sender, EventArgs e)

    // Clear the rows from the table.

    // 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 = "Gray";

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



<html xmlns="" >
   <title>HtmlTableRowCollection Example</title>

   <form id="form1" runat="server">

      <h3>HtmlTableRowCollection Example</h3>
       <table id="Table1" runat="server" 
       style="border-width: 1; border-color: Black">
               Cell 1
               Cell 2
               Cell 3
               Cell 4


      <br /><br />

      <input type="button" 
             value="Create New Table"



.NET Framework
Available since 1.1
Return to top