当您想以编程方式将 GridView 置于编辑模式时,请将EditIndex
属性设置为适当的行。
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.EditIndex = 1;
}
笔记:
1.) 如果您从标记绑定 gridview,使用 DataSourceControls 等SqlDataSource
,无需调用GridView.Bind()
.
2.) 如果您在运行时使用 DataSource 属性绑定 GridView,您应该重新绑定您的 gridView。
在您的问题中:surprisingly, its just changing row mode, where edit button is pressed.
,因为您正在单击Edit
按钮,所以如果您希望其他行处于编辑模式,请处理OnRowEditing
事件并设置属性:NewEditIndex
<asp:GridView ID="CustomersGrIdView"
OnRowEditing="CustomersGridView_Editing"
OnRowCommand="CustomersGridView_RowCommand"
.... />
protected void CustomersGridView_Editing(object sender, GridViewEditEventArgs e)
{
e.NewEditIndex = 3; // Tested in VS 2010, Framework 4
}
现在,无论单击任何行的Edit
按钮,您都会在编辑模式下找到第三行。[ 行编号从 0 开始 ]
这就是MSDN关于EditIndex
财产的说法。大多数时候,我们使用选项 C。
此属性通常仅用于以下场景,其中涉及特定事件的处理程序:
a. You want the GridView control to open in edit mode for a specific row
the first time that the page is displayed. To do this, you can set the
EditIndex property in the handler for the Load event of the Page class
or of the GridView control.
b. You want to know which row was edited after the row was updated.
To do this, you can retrieve the row index from the EditIndex property
in the RowUpdated event handler.
c. You are binding the GridView control to a data source by setting the
DataSource property programmatically. In this case you must set the
EditIndex property in the RowEditing and RowCancelingEdit event handlers.