Trouble getting value out of a GridView cell being edited
-
I have a GridView on my page that I am updating in the aspx.cs file instead of the aspx page. I have been able to get the SELECTs and DELETEs working OK, but the UPDATE command is giving me problems. I tracked it down to the fact that my code is not picking up the value of the cell I edit and then Update. For example, I have a GridView, populated from mssql, with the following columns Product Name | Ordered | Unit Price | Total All of the columns except for Ordered are set to read only. I have the template columns Edit and Delete. When I click Edit my template columns switch to Update and Cancel and the Ordered column's cell for the coressponding row turns into a textBox that I can type a new value in. When I click Update my page posts, but the value I typed in is not used. I think I am trying to access the value wrong. I am using Server.HtmlDecode to get the value out of the cell, but my suspicion is that it doesn't work for cells being edited. If that is the problem, what is the correct method?
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { GridViewRow row = GridView1.Rows[(Convert.ToInt32(e.CommandArgument))]; string productName = Server.HtmlDecode(row.Cells[0].Text); /*Various if/else if statements to catch the grid commands left out of here for brevity*/ else if (e.CommandName == "Update") { if (Session["orderIDandDate"] != null) { //the quantity value below isn't picking up the value in this row //the problem is most likely here--maybe using the wrong method quantity = Convert.ToInt32(Server.HtmlDecode(row.Cells[1].Text)); if (Convert.ToInt32(quantity) > 0) { SqlDataSource2.UpdateCommand = String.Format("UPDATE TABLE SET quantity = '{0}' WHERE (product = '{1}') AND (order = '{2}')", quantity, productName, orderID); SqlDataSource2.Update(); } } }
-
I have a GridView on my page that I am updating in the aspx.cs file instead of the aspx page. I have been able to get the SELECTs and DELETEs working OK, but the UPDATE command is giving me problems. I tracked it down to the fact that my code is not picking up the value of the cell I edit and then Update. For example, I have a GridView, populated from mssql, with the following columns Product Name | Ordered | Unit Price | Total All of the columns except for Ordered are set to read only. I have the template columns Edit and Delete. When I click Edit my template columns switch to Update and Cancel and the Ordered column's cell for the coressponding row turns into a textBox that I can type a new value in. When I click Update my page posts, but the value I typed in is not used. I think I am trying to access the value wrong. I am using Server.HtmlDecode to get the value out of the cell, but my suspicion is that it doesn't work for cells being edited. If that is the problem, what is the correct method?
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { GridViewRow row = GridView1.Rows[(Convert.ToInt32(e.CommandArgument))]; string productName = Server.HtmlDecode(row.Cells[0].Text); /*Various if/else if statements to catch the grid commands left out of here for brevity*/ else if (e.CommandName == "Update") { if (Session["orderIDandDate"] != null) { //the quantity value below isn't picking up the value in this row //the problem is most likely here--maybe using the wrong method quantity = Convert.ToInt32(Server.HtmlDecode(row.Cells[1].Text)); if (Convert.ToInt32(quantity) > 0) { SqlDataSource2.UpdateCommand = String.Format("UPDATE TABLE SET quantity = '{0}' WHERE (product = '{1}') AND (order = '{2}')", quantity, productName, orderID); SqlDataSource2.Update(); } } }
Ok, on about my 5th google search result page I found it :) If you do this from the GridView_RowUpdating function you can use e.NewValues to get the new value. Problem solved :)