Mazen Elkashef
Mazen Elkashef

Reputation: 3499

ASP.NET (GridView): GridView1.Rows[e.RowIndex].Cells[1].Text returns the original value not the value that I just entered

Editing Method:

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

Updating Method:

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int _selectedRowIndex = e.RowIndex;
        int _ameintyId = (int)GridView1.DataKeys[_selectedRowIndex].Value;
        string updatedAmenity = GridView1.Rows[_selectedRowIndex].Cells[0].Text;
    }

N.B: and a strange thing that I noticed, when I click edit it reloads the page but won't edit the row, so I click it again and it works .. and this happen everytime, it's not an exception or something!

Edit

First of all I'm using a BoundField .. I figured out that when I add a DataKey it created a new field for it which is the ID and that what caused the problem! so I just changed the 0 to 1 .. but now I'm facing a problem that the cell Text property returns the original value not the new value!

Upvotes: 0

Views: 15660

Answers (2)

David
David

Reputation: 1

string updatedAmenity = e.NewValues[0].ToString();

Upvotes: 0

Mazen Elkashef
Mazen Elkashef

Reputation: 3499

First Issue: To get the value I just entered instead of the original value

Updating Method: Used e.NewValues property

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int _selectedRowIndex = e.RowIndex;
        int _ameintyId = (int)GridView1.DataKeys[_selectedRowIndex].Value;
        string updatedAmenity = e.NewValues[0].ToString();
    }

Second Issue: Clicking twice to get the edit button to work

Editing Method: I just called my method that binds my grid

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

Thanks for offering your help =)

Upvotes: 2

Related Questions