다음을 통해 공유


TableCellCollection.GetCellIndex(TableCell) 메서드

정의

TableCell에 있는 지정된 TableCellCollection의 인덱스를 나타내는 값을 반환합니다.

public:
 int GetCellIndex(System::Web::UI::WebControls::TableCell ^ cell);
public int GetCellIndex (System.Web.UI.WebControls.TableCell cell);
member this.GetCellIndex : System.Web.UI.WebControls.TableCell -> int
Public Function GetCellIndex (cell As TableCell) As Integer

매개 변수

cell
TableCell

TableCell에 있는 인덱스를 가져올 TableCellCollection입니다.

반환

TableCell 내에 있는 지정된 TableCellCollection의 인덱스를 반환합니다. 기본값은 -1로, 지정된 인덱스와 일치하지 셀이 없음을 나타냅니다.

예제

다음 예제에서는 사용 GetCellIndex 하는 방법에 설명 합니다 선택한 셀의 열 인덱스를 표시 하는 속성입니다.

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<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();
                rw.Cells.Add(cel);
                counter++;
            }
            Table1.Rows.Add(rw);
        }

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

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

            List1.DataSource = a_row;
            List2.DataSource = a_column;
            List1.DataBind();
            List2.DataBind();
        }
    }

    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 " +
            Table1.Rows[row].Cells.GetCellIndex(cell).ToString();
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>TableCellCollection Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <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"
             OnClick="Button_Click"
             runat="server"/>
        <br />&nbsp;<br />
        <asp:Label id="Label1" runat="server"/>
    </p>

    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        Dim numrows As Integer = 5
        Dim numcells As Integer = 6
        Dim counter As Integer = 1
        Dim a_row As New ArrayList()
        Dim a_column As New ArrayList()

        ' Create a table.
        Dim rowNum As Integer
        For rowNum = 0 To numrows - 1
            Dim rw As New TableRow()
            Dim cellNum As Integer
            For cellNum = 0 To numcells - 1
                Dim cel As New TableCell()
                cel.Text = counter.ToString()
                rw.Cells.Add(cel)
                counter += 1
            Next
            Table1.Rows.Add(rw)
        Next

        If Not IsPostBack Then
            ' Create a DropDownList for the number of rows.
            For rowNum = 0 To numrows - 1
                a_row.Add(rowNum.ToString())
            Next

            ' Create a DropDownList for the number of columns.
            Dim cellNum As Integer
            For cellNum = 0 To numcells - 1
                a_column.Add(cellNum.ToString())
            Next

            List1.DataSource = a_row
            List2.DataSource = a_column
            List1.DataBind()
            List2.DataBind()
        End If
    End Sub

    Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim row As Integer = List1.SelectedIndex
        Dim column As Integer = List2.SelectedIndex
        Dim cell As TableCell = Table1.Rows(row).Cells(column)

        Label1.Text = "The column index of the selected cell is " & _
            Table1.Rows(row).Cells.GetCellIndex(cell).ToString()
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    <h3>TableCellCollection Example</h3>
    <asp:Table id="Table1" runat="server"/>
    <br /><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"
             OnClick="Button_Click"
             runat="server"/>
        <br />&nbsp;<br />
        <asp:Label id="Label1" runat="server"/>
    </p>

    </div>
    </form>
</body>
</html>

설명

이 메서드를 사용하여 에 지정된 TableCell 의 인덱스 TableCellCollection확인 지정된 TableCell 를 찾을 수 없으면 -1의 인덱스가 반환됩니다.

적용 대상

추가 정보