简体   繁体   English

重新绑定gridview与编辑模式有什么关系?

[英]What rebinding a gridview has to do with edit mode?

Gridview remains in EDIT mode when i don't rebind it, why ? 当我不重新绑定Gridview时,它仍处于EDIT模式,为什么? but when i rebind gridview that it exits out of eidt mode successfully, why ? 但是当我重新绑定它成功退出eidt模式的gridview时,为什么呢?

code: 码:

protected void gvTest_EditCancel(Object sender, GridViewCancelEditEventArgs e) 
{
    gvTest.EditIndex = -1;
  //  connectToDb();
    Response.Write("<br/>"+ "Editing cancelled");
}`

public void connectToDb() 
{
    SqlConnection sqlcon = new SqlConnection(constrng);
    String com1 = "select * from login";
    SqlCommand sqlcom = new SqlCommand(com1, sqlcon);
    SqlDataAdapter sqlda = new SqlDataAdapter(sqlcom);
    DataSet ds = new DataSet();

    try
    {
        sqlcon.Open();
        sqlda.Fill(ds);
        gvTest.DataSource = ds;
        gvTest.DataBind();
    }
    catch (Exception exc)
    {
        Response.Write(exc.ToString());
    }
    finally
    {
        sqlcon.Close();
    }
}

but when call ConnectToDb() to rebind then it successfully exists out of edit mode, what a rebinding has to do with EDITING or exiting of it ? 但是,当调用ConnectToDb()重新绑定时,它成功退出了编辑模式,重新绑定与EDITING或退出它有什么关系?

It is the EditIndex that telling the gridview that it is in edit mode. 正是EditIndex告诉GridView它处于编辑模式。

The zero-based index of the row to edit. 要编辑的行的从零开始的索引。 The default is -1, which indicates that no row is being edited. 缺省值为-1,表示没有行被编辑。

MSDN . MSDN

When you edit the gridview, EditIndex is set to the GridView RowIndex, which is always 0 or greater than 0. so the GridView stays in edit mode. 当您编辑gridview时,EditIndex设置为GridView RowIndex,该值始终为0或大于0。因此GridView保持在编辑模式。 To exit edit mode, you need to set the index to -1. 要退出编辑模式,您需要将索引设置为-1。 When you cancel edit, it sets this index to -1. 取消编辑时,它将此索引设置为-1。

In RowCancelingEdit we can set the editindex to -1. 在RowCancelingEdit中,我们可以将editindex设置为-1。 But it will not change the row to normal mode until we rebind the row. 但是,除非我们重新绑定该行,否则它不会将行更改为普通模式。 When we rebind the gridview, it resets the row(OnRowDataBound). 当我们重新绑定gridview时,它将重置行(OnRowDataBound)。 (But it doesn't reset the EditIndex, I was wrong about this). (但是它不会重置EditIndex,对此我错了)。 That's why whenever we do any row editing or deleting, we need to rebind the gridview, 因此,每当进行任何行编辑或删除操作时,都需要重新绑定gridview,

So, 所以,

  1. To exit edit mode, we have to set EditIndex = -1 要退出编辑模式,我们必须设置EditIndex = -1
  2. EditIndex change will not reflect until we rebind the GridView. 在重新绑定GridView之前,EditIndex的更改不会反映出来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM