Lucas Verhoest
Lucas Verhoest

Reputation: 275

Gridview row updating button not working

My gridview row updating button is not working when I press edit and then press on the button to apply the changes it doesn't do anything. Am I missing something?

My code looks like this

C#

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    string afspraak = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("EditAfspraak"))).Text;
    string uitleg = ((TextBox)(GridView1.Rows[e.RowIndex].FindControl("EditUitleg"))).Text;
    int id = Convert.ToInt32(((TextBox)(GridView1.Rows[e.RowIndex].FindControl("EditID"))).Text);
    _controller.RecordUpdatenTblAfspraken(afspraak, uitleg, id);
}

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

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
}

ASP.NET

Couldn't paste it correctly so it's here:

https://pastebin.com/S3S09nLL

Highlighted button isn't working enter image description here

Upvotes: 1

Views: 1749

Answers (1)

VDWWD
VDWWD

Reputation: 35524

You have to rebind the grid after you change the EditIndex.

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

    GridView1.DataSource = mijnDatabaseBron;
    GridView1.DataBind();
}

The same goes for GridView1_RowCancelingEdit and when update has succeeded in GridView1_RowUpdating. This is assuming you added the OnRowEditing, OnRowCancelingEdit and OnRowUpdating events to the GridView.

Also as user7415073 commented, the binding of the GridView must be inside an IsPostBack check.

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        GridView1.DataSource = mijnDatabaseBron;
        GridView1.DataBind();
    }
}

And see my answer here as to how you can easily pass the EditID to code behind.

Get old value of label inside GridView during RowUpdating (ASP.NET GridView) C#

Upvotes: 2

Related Questions