I'm having a problem with editing values of a gridview and them being updated back to the gridview. At the moment the code gets the old value of the textbox and I'm assuming I've made an error in trying to obtain the new value.

I've highlighted what I think to be the problem.

protected void GVCheckout_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //Retrieve the table from the session object.
        DataTable dt = (DataTable)Session["Cart"];

        //Update the values.
        GridViewRow row = gv_checkout.Rows[e.RowIndex];

        [B]string quantity = ((TextBox)(gv_checkout.Rows[e.RowIndex].Cells[6].Controls[0])).Text.ToString();[/B]
        dt.Rows[row.DataItemIndex]["Quantity"] = int.Parse(quantity);
        dt.Rows[row.DataItemIndex]["Airbrushing"] = ((CheckBox)(row.Cells[7].Controls[0])).Checked;
    
        //Reset the edit index.
        //gv_checkout.DataSource = "Cart";
        //gv_checkout.DataMember = "Cart";
        //gv_checkout.DataBind();
        gv_checkout.EditIndex = -1;
        
        //Bind data to the GridView control.
        BindData();
    }

    private void BindData()
    {
        gv_checkout.DataSource = Session["Cart"];
        gv_checkout.DataBind();
    }

Thanks

jbennet commented: have a load of reputation +22

Recommended Answers

All 2 Replies

According to MSDN
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx

RowEditing
Occurs when a row's Edit button is clicked, but before the GridView control enters edit mode. This event is often used to cancel the editing operation.

RowUpdated
Occurs when a row's Update button is clicked, but after the GridView control updates the row. This event is often used to check the results of the update operation.

RowUpdating

Occurs when a row's Update button is clicked, but before the GridView control updates the row. This event is often used to cancel the updating operation.

Specificially look at
http://msdn.microsoft.com/en-us/library/ms972948.aspx

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewupdateeventargs.newvalues.aspx

In the RowUpdating method

void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
  {

    // Iterate through the NewValues collection
    foreach (DictionaryEntry entry in e.NewValues)
    {
      // The thing to work with is e.NewValues[entry.Key]  
    // To get the changed value its e.g. entry.Value.ToString();

    }

  }

You could put your DB code in there? Its just a bit naff going from those examples because you are working from a basket defined in code, not a SqlDataSource in the designer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.