TableCellCollection.GetCellIndex Method (TableCell)


Returns a value that represents the index of the specified TableCell from the TableCellCollection.

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

public int GetCellIndex(
	TableCell cell


Type: System.Web.UI.WebControls.TableCell

The TableCell to get the index of in the TableCellCollection.

Return Value

Type: System.Int32

The index of the specified TableCell within the TableCellCollection. The default is -1, which indicates that a match has not been found.

Use this method to determine the index of the specified TableCell in the TableCellCollection. If the specified TableCell is not found, an index of -1 is returned.

The following example demonstrates how to use the GetCellIndex property to display the column index of the selected cell.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 

<script runat="server">

    void Page_Load(Object sender, EventArgs e)

        int numrows = 5;
        int numcells = 6;
        int counter = 1;
        ArrayList a_row = new ArrayList();
        ArrayList a_column = new ArrayList();

        // Create a table.
        for (int rowNum = 0; rowNum < numrows; rowNum++)
            TableRow rw = new TableRow();
            for (int cellNum = 0; cellNum < numcells; cellNum++)
                TableCell cel = new TableCell();
                cel.Text = counter.ToString();

        if (!IsPostBack)
            // Create a DropDownList for the number of rows.
            for (int k = 0; k < numrows; k++)

            // Create a DropDownList for the number of columns.
            for (int l = 0; l < numcells; l++)

            List1.DataSource = a_row;
            List2.DataSource = a_column;

    void Button_Click(object sender, EventArgs e)
        int row = List1.SelectedIndex;
        int column = List2.SelectedIndex;
        TableCell cell = Table1.Rows[row].Cells[column];

        Label1.Text = "The column index of the selected cell is " +


<html xmlns="" >
<head id="Head1" runat="server">
    <title>TableCellCollection Example</title>
    <form id="form1" runat="server">

    <h3>TableCellCollection Example</h3>
    <asp:Table id="Table1" runat="server"/>
    <br />&nbsp;<br />
    <p style="text-align:center">
        Select a cell:
        <br />&nbsp;<br />
        Row: <asp:DropDownList id="List1" runat="server"/>
        Column: <asp:DropDownList id="List2" runat="server"/>
        <br />&nbsp;<br />
        <asp:Button id="Button1"
             Text="Get Index"
        <br />&nbsp;<br />
        <asp:Label id="Label1" runat="server"/>


.NET Framework
Available since 1.1
Return to top