1

我正在搜索如何将 gridview 行的模式更改为编辑模式。而且,我得到了一个答案,

Gridview1.EditIndex =1;

所以,我把它放到 Rowcommand 事件中。我认为它只是改变第一行的模式,而不是在任何行的任何地方按下编辑按钮。但令人惊讶的是,它只是改变了行模式,它按下了编辑按钮。谁能告诉我这是如何工作的?


我正在单击编辑按钮,但它是自定义链接按钮,它具有 CommandName="Edit",然后我将此代码放在我的 .cs 文件中

 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
 {
    if (e.CommandName == "Edit")
    {
        GridView1.EditIndex = 2; // tested in VS 2008, .NET 3.5
        // Here doesn't matter, if I write 1,2 or any number, but when I click Edit button from  row, then same row only go into Edit mode.
    }
   ------
 }
4

1 回答 1

1

当您想以编程方式将 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.
于 2013-09-03T09:22:17.497 回答