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 初めて表示されるときに、コントロールを特定の行の編集モードで開く必要があります。 これを行うには、 クラスまたは コントロールの EditIndex イベントPageの ハンドラーLoadで プロパティをGridView設定できます。

  • 行が更新された後に編集された行を知りたい場合。 これを行うには、イベント ハンドラーの プロパティから EditIndex 行インデックスを RowUpdated 取得できます。

  • コントロールを GridView データ ソースにバインドするには、 プロパティを DataSource プログラムで設定します。 この場合、 イベント ハンドラーと RowCancelingEdit イベント ハンドラーで プロパティをRowEditing設定EditIndexする必要があります。

ポストバックの後、または イベントより後に発生するイベントのハンドラーで プロパティをLoad設定EditIndexした場合、GridViewコントロールが指定された行の編集モードに入らない可能性があります。 他のイベント ハンドラーでこのプロパティの値を読み取った場合、インデックスには編集中の行が反映されるとは限りません。

コントロールが編集モードになる前にGridViewユーザーが [編集] ボタンまたはハイパーリンクをクリックした行を確認するには、イベント ハンドラーの オブジェクトの GridViewEditEventArgs プロパティからNewEditIndex行インデックスをRowEditing取得します。

ユーザーが GridView [編集] ボタンまたはハイパーリンクをクリックした後にコントロールが編集モードに入らないようにするには、イベント ハンドラーで オブジェクトの GridViewEditEventArgs プロパティを RowEditingtrue設定Cancelします。

適用対象

こちらもご覧ください