Insert Method

HtmlTableCellCollection.Insert Method (Int32, HtmlTableCell)


Adds the specified HtmlTableCell object at the specified index location of the HtmlTableCellCollection collection.

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

public void Insert(
	int index,
	HtmlTableCell cell


Type: System.Int32

The location in the HtmlTableCellCollection at which to add the HtmlTableCell.

Type: System.Web.UI.HtmlControls.HtmlTableCell

The HtmlTableCell to add to the HtmlTableCellCollection.

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

The following code example demonstrates how to use the Insert method to dynamically insert a cell of a table, represented by an HtmlTableCell object, in an HtmlTableCellCollection 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 = "Gray";

      // 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>HtmlTableCellCollection Example</title>

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

      <h3>HtmlTableCellCollection Example</h3>

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

      <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
© 2016 Microsoft