GridView.EditIndex 속성

정의

편집할 행의 인덱스를 가져오거나 설정합니다.

public:
 virtual property int EditIndex { int get(); void set(int value); };
public virtual int EditIndex { get; set; }
member this.EditIndex : int with get, set
Public Overridable Property EditIndex As Integer

속성 값

편집할 행의 0부터 시작하는 인덱스입니다. 기본값은 -1로, 편집하고 있는 행이 없음을 나타냅니다.

예외

지정된 인덱스가 -1보다 작은 경우

예제

다음 예제에서는 사용 하는 방법을 보여 줍니다.는 컨트롤에서 EditIndex 편집 된 후 업데이트 된 행을 확인 하는 속성입니다 GridView . 업데이트가 성공했음을 나타내는 메시지가 표시됩니다.


<%@ 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 CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
    
    // Clear the message label when the user enters edit mode.
    if (e.CommandName == "Edit")
    {
      Message.Text = "";
    }
    
  }

  void CustomersGridView_RowUpdated(Object sender, GridViewUpdatedEventArgs e)
    {
   
        // The update operation was successful. Retrieve the row being edited.
        int index = CustomersGridView.EditIndex;
        GridViewRow row = CustomersGridView.Rows[index];
        
        // Notify the user that the update was successful.
        Message.Text = "Updated record " + row.Cells[1].Text + ".";
    
    }

  void CustomersGridView_RowCancelingEdit(Object sender, GridViewCancelEditEventArgs e)
    {
   
        // The update operation was canceled. Display the appropriate message.
        Message.Text = "Update operation canceled.";
    
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView Rows Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView Rows Example</h3>
            
      <asp:label id="Message"
        forecolor="Red"
        runat="server"/>
                
      <br/>    
            
      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView"
        allowpaging="true" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogenerateeditbutton="true"
        datakeynames="CustomerID"
        onrowcommand="CustomersGridView_RowCommand"
        onrowupdated="CustomersGridView_RowUpdated"
        onrowcancelingedit="CustomersGridView_RowCancelingEdit"  
        runat="server">
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </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 CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
    
    ' Clear the message label when the user enters edit mode.
    If e.CommandName = "Edit" Then
      Message.Text = ""
    End If
    
  End Sub
  
  Sub CustomersGridView_RowUpdated(ByVal sender As Object, ByVal e As GridViewUpdatedEventArgs)
   
    ' The update operation was successful. Retrieve the row being edited.
    Dim index As Integer = CustomersGridView.EditIndex
    Dim row As GridViewRow = CustomersGridView.Rows(index)
        
    ' Notify the user that the update was successful.
    Message.Text = "Updated record " & row.Cells(1).Text + "."
    
  End Sub

  Sub CustomersGridView_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs)
   
    ' The update operation was canceled. Display the appropriate message.
    Message.Text = "Update operation canceled."
    
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>GridView Rows Example</title>
</head>
<body>
    <form id="form1" runat="server">
        
      <h3>GridView Rows Example</h3>
            
      <asp:label id="Message"
        forecolor="Red"
        runat="server"/>
                
      <br/>    
            
      <!-- The GridView control automatically sets the columns     -->
      <!-- specified in the datakeynames property as read-only.    -->
      <!-- No input controls are rendered for these columns in     -->
      <!-- edit mode.                                              -->
      <asp:gridview id="CustomersGridView"
        allowpaging="true" 
        datasourceid="CustomersSqlDataSource" 
        autogeneratecolumns="true"
        autogenerateeditbutton="true"
        datakeynames="CustomerID"
        onrowcommand="CustomersGridView_RowCommand"
        onrowupdated="CustomersGridView_RowUpdated"
        onrowcancelingedit="CustomersGridView_RowCancelingEdit"  
        runat="server">
      </asp:gridview>
            
      <!-- This example uses Microsoft SQL Server and connects  -->
      <!-- to the Northwind sample database. Use an ASP.NET     -->
      <!-- expression to retrieve the connection string value   -->
      <!-- from the Web.config file.                            -->
      <asp:sqldatasource id="CustomersSqlDataSource"  
        selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]"
        updatecommand="Update Customers SET CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country WHERE (CustomerID = @CustomerID)"
        deletecommand="Delete from Customers where CustomerID = @CustomerID"
        connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>"
        runat="server">
      </asp:sqldatasource>
            
    </form>
  </body>
</html>

설명

행 인덱스는 0부터 시작합니다(첫 번째 행은 행 0임).

이 속성은 일반적으로 특정 이벤트에 대한 처리기를 포함하는 다음 시나리오에서만 사용됩니다.

  • 페이지가 GridView 처음으로 표시될 때 컨트롤이 특정 행에 대해 편집 모드로 열리도록 합니다. 이렇게 하려면 클래스 또는 GridView 컨트롤의 이벤트에 대 한 Load 처리기에서 Page 속성을 설정할 EditIndex 수 있습니다.

  • 행이 업데이트된 후 편집된 행을 알고 싶습니다. 이렇게 하려면 이벤트 처리기의 속성에서 EditIndex 행 인덱스 를 검색할 RowUpdated 수 있습니다.

  • 속성을 프로그래밍 방식으로 설정하여 컨트롤을 데이터 원본에 바인딩 GridView 합니다 DataSource . 이 경우 및 RowCancelingEdit 이벤트 처리기에서 RowEditing 속성을 설정 EditIndex 해야 합니다.

설정 하는 경우 EditIndex 는 포스트백 후 또는 이벤트보다 LoadGridView 나중에 발생 하는 이벤트에 대 한 처리기에서 컨트롤 지정 된 행에 대 한 편집 모드를 입력 하지 않을 수 있습니다. 다른 이벤트 처리기에서 이 속성의 값을 읽는 경우 인덱스는 편집 중인 행을 반영하도록 보장되지 않습니다.

컨트롤이 편집 모드로 전환되기 전에 GridView 사용자가 편집 단추 또는 하이퍼링크를 클릭한 행을 확인하려면 이벤트 처리기에서 개체의 GridViewEditEventArgs 속성에서 NewEditIndexRowEditing 행 인덱스를 검색할 수 있습니다.

사용자가 편집 단추 또는 하이퍼링크를 클릭한 후 컨트롤이 편집 모드로 전환되지 않도록 GridView 하려면 이벤트 처리기에서 RowEditing 개체의 GridViewEditEventArgs 속성을 로 true 설정합니다.Cancel

적용 대상

추가 정보