GridView.EditIndex 属性

获取或设置要编辑的行的索引。

 

属性值

要编辑的行的从零开始的索引。默认值为 -1,指示没有正在编辑的行。

异常类型 条件

ArgumentOutOfRangeException

指定的索引小于 -1。

使用 EditIndex 属性以编程方式指定或确定要编辑 GridView 控件中的哪一行。当此属性被设置为控件中的某一行的索引时,该行进入编辑模式。在编辑模式中,非只读行中的每一个列字段显示对应于该字段的数据类型的输入控件,如 TextBox 控件。这允许用户修改该字段的值。若要退出编辑模式,请将此属性设置为 -1。

Note注意

GridView 控件有自动设置此属性的内置编辑功能。此属性通常仅用在当您需要以编程方式确定正在编辑哪一行时,或者用在当您将自己的自定义编辑功能添加到 GridView 控件时。

下面的代码示例演示如何使用 EditIndex 属性确定正在编辑 GridView 控件中的哪一行。在行被更新之后,会显示一条消息指示更新成功。

 

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

<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>
  <body>
    <form 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>

 

posted @ 2009-04-17 11:08  minmin8110  阅读(843)  评论(0)    收藏  举报