HtmlTableRowCollection.Insert Method (Int32, HtmlTableRow)


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

Adds an HtmlTableRow object to the specified location in the collection.

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

public void Insert(
	int index,
	HtmlTableRow row


Type: System.Int32

The location in the HtmlTableRowCollection at which to add the HtmlTableRow.

Type: System.Web.UI.HtmlControls.HtmlTableRow

The HtmlTableRow to add to the HtmlTableRowCollection.

Use the Insert method to add the specified HtmlTableRow object to an HtmlTableRowCollection collection at the specified index. If you simply want to append an HtmlTableRow to the end of the collection, use the Add method.

The following code example demonstrates how to use the Insert method to insert a row of a table, represented by an HtmlTableRow object, into an HtmlTableRowCollection collection.

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

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

  void Page_Load(Object sender, EventArgs e)

    // Get the number of rows and columns selected by the user.
    int numrows = Convert.ToInt32(Select1.Value);
    int numcells = Convert.ToInt32(Select2.Value);

    // Iterate through the rows.
    for (int j = 0; j < numrows; 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 < numcells; 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 " +
        row.Cells.Insert(i, cell);
      Table1.Rows.Insert(j, row);


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

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

      <h3>HtmlTableRowCollection Example</h3>

      <table id="Table1" 
             style="padding:5; border-width:1; border-color:Black"/>

      <hr />

      Select the number of rows and columns to create: <br /><br />

      Table rows:
      <select id="Select1" 

         <option value="1">1</option>
         <option value="2">2</option>
         <option value="3">3</option>
         <option value="4">4</option>
         <option value="5">5</option>



      Table cells:
      <select id="Select2" 

         <option value="1">1</option>
         <option value="2">2</option>
         <option value="3">3</option>
         <option value="4">4</option>
         <option value="5">5</option>


      <br /><br />

      <input type="submit" 
             value="Generate Table" 



.NET Framework
Available since 1.1
Return to top