Grid not updating on edit button press

时间:2023-01-15 05:32:20

I have a gridview.

我有一个gridview。

I am trying to edit it, but value is not getting updated.

我正在尝试编辑它,但价值没有得到更新。

My Code:

 protected void Page_Load(object sender, EventArgs e)
        {

            con = new SqlConnection("Data Source=192.168.51.71;Initial Catalog=WebBasedNewSoft;User ID=sa;password=prabhu");

            BindGrid();
        }
        private void BindGrid()
        {
            try
            {
                da = new SqlDataAdapter("select * from emp", con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds.Tables[0];
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
            }
        }
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int index = GridView1.EditIndex;

            GridViewRow row = GridView1.Rows[index];

            string eName = ((TextBox)row.Cells[2].Controls[0]).Text.ToString().Trim();

            try
            {
                con.Open();
                cmd = new SqlCommand("update emp set empName='" + eName + "'", con);
                cmd.ExecuteNonQuery();
                con.Close();
                BindGrid();
                GridView1.EditIndex = -1;
            }
            catch (Exception ex)
            {
            }
            finally
            {

            }

        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            BindGrid();
        }
    }

Please let me know the point where i am making mistake.

请让我知道我犯错的地方。

2 个解决方案

#1


2  

You can try like this...becuase...when you click on edit button your pageload event is called first....and on pageload you bind the gridview again...so its edit index is lost because it binded again...and it set edit index to -1 each and everytime...

您可以尝试这样...因为...当您单击编辑按钮时,您的pageload事件首先被调用....并且在页面加载时您再次绑定gridview ...因此它的编辑索引会丢失,因为它再次绑定。 ..并且每次都将编辑索引设置为-1 ...

 protected void Page_Load(object sender, EventArgs e)
        {
        if(!Page.IsPostBack)
              {
             con = new SqlConnection("Data Source=192.168.51.71;Initial            Catalog=WebBasedNewSoft;User ID=sa;password=prabhu");

            BindGrid();
        }
}

Edit1: OP After Update Edit Mode Doesnt goes.. You Have to Set gridview Edit index before Its binding like below....

编辑1:OP后更新编辑模式不会..你必须设置gridview编辑索引之前它的绑定如下....

try
            {
                con.Open();
                cmd = new SqlCommand("update emp set empName='" + eName + "'", con);
                cmd.ExecuteNonQuery();
                con.Close();
                GridView1.EditIndex = -1;//Put this line Before the Binding of GridView
                BindGrid();

            }

#2


3  

use BindGrid() inside Page.IsPostBack as follows on Page_Load event

在Page_Load事件中使用Page.IsPostBack中的BindGrid()如下所示

if(!Page.IsPostBack)
{
     BindGrid();
}

Edit 1

I think the following line should work

我认为以下行应该有效

   BindGrid();
   GridView1.EditIndex = -1;

As it is not working see is there any error in the catch block.

由于它不工作,请看catch块中是否有任何错误。

  catch (Exception ex)
  {
       Response.Write(ex.Message);
  }

See weather there is some error or not?

看天气有没有错误?

#1


2  

You can try like this...becuase...when you click on edit button your pageload event is called first....and on pageload you bind the gridview again...so its edit index is lost because it binded again...and it set edit index to -1 each and everytime...

您可以尝试这样...因为...当您单击编辑按钮时,您的pageload事件首先被调用....并且在页面加载时您再次绑定gridview ...因此它的编辑索引会丢失,因为它再次绑定。 ..并且每次都将编辑索引设置为-1 ...

 protected void Page_Load(object sender, EventArgs e)
        {
        if(!Page.IsPostBack)
              {
             con = new SqlConnection("Data Source=192.168.51.71;Initial            Catalog=WebBasedNewSoft;User ID=sa;password=prabhu");

            BindGrid();
        }
}

Edit1: OP After Update Edit Mode Doesnt goes.. You Have to Set gridview Edit index before Its binding like below....

编辑1:OP后更新编辑模式不会..你必须设置gridview编辑索引之前它的绑定如下....

try
            {
                con.Open();
                cmd = new SqlCommand("update emp set empName='" + eName + "'", con);
                cmd.ExecuteNonQuery();
                con.Close();
                GridView1.EditIndex = -1;//Put this line Before the Binding of GridView
                BindGrid();

            }

#2


3  

use BindGrid() inside Page.IsPostBack as follows on Page_Load event

在Page_Load事件中使用Page.IsPostBack中的BindGrid()如下所示

if(!Page.IsPostBack)
{
     BindGrid();
}

Edit 1

I think the following line should work

我认为以下行应该有效

   BindGrid();
   GridView1.EditIndex = -1;

As it is not working see is there any error in the catch block.

由于它不工作,请看catch块中是否有任何错误。

  catch (Exception ex)
  {
       Response.Write(ex.Message);
  }

See weather there is some error or not?

看天气有没有错误?