Gridview: editing one cell and updating based on calculationhttp://forums.asp.net/t/1636083.aspx/1?Gridview+editing+one+cell+and+updating+based+on+calculationTue, 04 Jan 2011 15:06:52 -050016360834224274http://forums.asp.net/p/1636083/4224274.aspx/1?Gridview+editing+one+cell+and+updating+based+on+calculationGridview: editing one cell and updating based on calculation <p>I have an ecomerce site I am trying to update a cell value for Cases when the value for totalpounds is changed. and then update in the data base and return the new gridview. here is my code - I have been working on the onupdateing to try and get this to work but as of yet have not been successful. I'm using vb.</p> <p>thanks</p> <p>richard</p> <pre class="prettyprint">&lt;%@ Page Language=&quot;VB&quot; MasterPageFile=&quot;~/Store.master&quot; Title=&quot;Shopping Cart&quot; %&gt; &lt;script runat=&quot;server&quot;&gt; Private _total As Decimal = 0 Private _totalLbs As Decimal = 0 Private _totalquantity As Integer Protected Sub rowUpdateing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Dim records(e.NewValues.Count - 1) As DictionaryEntry Dim entry As DictionaryEntry Dim totalLbs As String Dim lblTotalQuantity As Integer Dim lbs As Integer e.NewValues.CopyTo(records, 0) For Each entry In records grdShoppingCart.Rows(grdShoppingCart.EditIndex).Cells(2).FindControl(&quot;lblLbs&quot;).ToString() lbs = CType(grdShoppingCart.Rows(grdShoppingCart.EditIndex).Cells(2).FindControl(&quot;lblLbs&quot;), Label).Text() totalLbs = entry.Key.ToString lblTotalQuantity = totalLbs / lbs lblTotalQuantity = CType(grdShoppingCart.Rows(grdShoppingCart.EditIndex).Cells(3).FindControl(&quot;lblTotalQuantity&quot;), Label).Text() DataBind() Next End Sub ''' &lt;summary&gt; ''' Calculate running total as rows are bound to the GridView ''' &lt;/summary&gt; Protected Sub grdShoppingCart_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then Dim price As Decimal = CType(DataBinder.Eval(e.Row.DataItem, &quot;Price&quot;), Decimal) Dim lbs As Integer = CType(DataBinder.Eval(e.Row.DataItem, &quot;Lbs&quot;), Integer) Dim totallbs As Integer = CType(DataBinder.Eval(e.Row.DataItem, &quot;TotalLbs&quot;), Integer) _total &#43;= (price * totallbs) _totalLbs &#43;= (totallbs) End If End Sub ''' &lt;summary&gt; ''' Show price total and check out link ''' &lt;/summary&gt; Protected Sub grdShoppingCart_DataBound(ByVal sender As Object, ByVal e As EventArgs) If _total = 0 And _totalLbs = 0 Then lnkCheckOut.Visible = False Else Dim lblTotal As Label = CType(grdShoppingCart.FooterRow.FindControl(&quot;lblTotal&quot;), Label) Dim lblTotalLbs As Label = CType(grdShoppingCart.FooterRow.FindControl(&quot;lblTotalLbs&quot;), Label) lblTotal.Text = _total.ToString(&quot;c&quot;) lblTotalLbs.Text = _totalLbs.ToString() &amp; &quot; lbs&quot; End If End Sub &lt;/script&gt; &lt;asp:Content ID=&quot;Content1&quot; ContentPlaceHolderID=&quot;contentMiddle&quot; Runat=&quot;Server&quot;&gt; &lt;asp:GridView id=&quot;grdShoppingCart&quot; DataSourceID=&quot;srcCart&quot; DataKeyNames=&quot;id&quot; AutoGenerateColumns=&quot;False&quot; AutoGenerateDeleteButton=&quot;True&quot; AutoGenerateEditButton=&quot;True&quot; EmptyDataText=&quot;There are no items in your shopping cart. Login to view your saved shopping cart.&quot; CssClass=&quot;cartBig&quot; AlternatingRowStyle-CssClass=&quot;cartAlternating&quot; GridLines=&quot;None&quot; ShowFooter=&quot;True&quot; Runat=&quot;server&quot; OnDataBound=&quot;grdShoppingCart_DataBound&quot; OnRowUpdating=&quot;rowUpdateing&quot; OnRowDataBound=&quot;grdShoppingCart_RowDataBound&quot;&gt; &lt;AlternatingRowStyle CssClass=&quot;cartAlternating&quot;&gt;&lt;/AlternatingRowStyle&gt; &lt;Columns&gt; &lt;asp:TemplateField HeaderText=&quot;Product Name&quot;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&quot;Label2&quot; runat=&quot;server&quot; Text='&lt;%# Eval(&quot;Name&quot;) %&gt;' Width=&quot;200&quot;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;HeaderStyle Width=&quot;100px&quot; /&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&quot;lbs&quot;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&quot;lblLbs&quot; runat=&quot;server&quot; Text='&lt;%# Bind(&quot;Lbs&quot;) %&gt;'&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:Label ID=&quot;lblLbs1&quot; runat=&quot;server&quot; Text='&lt;%# Eval(&quot;Lbs&quot;) %&gt;'&gt;&lt;/asp:Label&gt; &lt;/EditItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&quot;Cases&quot;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&quot;lbl3&quot; runat=&quot;server&quot; Text='&lt;%# Bind(&quot;TotalQuantity&quot;) %&gt;'&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:Label ID=&quot;lblTotalQuantity&quot; runat=&quot;server&quot; Text='&lt;%# Eval(&quot;TotalQuantity&quot;) %&gt;'&gt;&lt;/asp:Label&gt; &lt;/EditItemTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&quot;Total Pounds&quot;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&quot;Label3&quot; runat=&quot;server&quot; Text='&lt;%# Bind(&quot;TotalLbs&quot;) %&gt;'&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;EditItemTemplate&gt; &lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot; Text='&lt;%# Bind(&quot;TotalLbs&quot;) %&gt;' Width=&quot;50&quot; ForeColor=&quot;Red&quot; BackColor=&quot;Yellow&quot; BorderStyle=&quot;Inset&quot;&gt; &lt;/asp:TextBox&gt; &lt;/EditItemTemplate&gt; &lt;FooterTemplate&gt; &lt;asp:Label ID=&quot;lblTotalLbs&quot; Runat=&quot;server&quot; BackColor=&quot;#CCFFCC&quot; /&gt; &lt;/FooterTemplate&gt; &lt;/asp:TemplateField&gt; &lt;asp:TemplateField HeaderText=&quot;Price per pound&quot;&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID=&quot;Label1&quot; runat=&quot;server&quot; Text='&lt;%# Bind(&quot;TotalLbs&quot;) %&gt;' Width=&quot;80&quot;&gt;&lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;ItemTemplate&gt; &lt;%# Eval(&quot;Price&quot;, &quot;{0:c3}&quot;) %&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; &lt;asp:Label ID=&quot;lblTotal&quot; Runat=&quot;server&quot; BackColor=&quot;#CCFFCC&quot; /&gt; &lt;/FooterTemplate&gt; &lt;FooterStyle CssClass=&quot;cartTotalFooter&quot; /&gt; &lt;FooterStyle CssClass=&quot;cartTotalFooter&quot; /&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;br /&gt;&lt;br /&gt; &lt;asp:Hyperlink id=&quot;lnkCheckOut&quot; Text=&quot;Check Out&quot; NavigateUrl=&quot;~/CheckOut/Default.aspx&quot; Runat=&quot;server&quot; /&gt; &lt;asp:ObjectDataSource id=&quot;srcCart&quot; TypeName=&quot;AspNetUnleashed.ShoppingCart&quot; SelectMethod=&quot;Select&quot; UpdateMethod=&quot;Update&quot; DeleteMethod=&quot;Delete&quot; Runat=&quot;server&quot; /&gt; &lt;/asp:Content&gt; &lt;asp:Content ContentPlaceHolderID=&quot;contentRight&quot; runat=&quot;server&quot; /&gt;</pre> <p><br> &nbsp;</p> 2010-12-23T21:10:03-05:004226428http://forums.asp.net/p/1636083/4226428.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Hi richard1970,</p> <p>You can save the original total value in Session in the RowDataBound or DataBound event. It depends on which total value you means. Then in the start of RowUpdating event you can check the current total value with the original one from the Session to decide whether do updating or not.</p> <p>Thanks,</p> 2010-12-27T07:37:18-05:004227413http://forums.asp.net/p/1636083/4227413.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>&nbsp;ok, how do I do that in the code?</p> <p>Thanks</p> 2010-12-27T21:42:21-05:004227516http://forums.asp.net/p/1636083/4227516.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Hi richard1970,</p> <p>I am not sure which total value you want to check whether it is changed or not. Just save the original one into Session. When you need to compare, then get it from Session and compare with the new one. Just put the related code into the related event. nothing special. Like:</p> <p><em>'for saving<br> </em>Session(&quot;total&quot;) = original_total</p> <p><em>'for comparing<br> </em>Dim o_total As Integer = Convert.ToInt32(Session(&quot;total&quot;))<br> If o_total &lt;&gt; new_total Then<br> &nbsp;&nbsp;&nbsp;<em> 'doing something<br> </em>End If</p> <p>Thanks,</p> 2010-12-28T01:29:47-05:004227538http://forums.asp.net/p/1636083/4227538.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Hi &nbsp;Qin Dian Tang</p> <p>What I was trying to do is, when my customer clicks on the edit in the gridview row, he can change only the total number of pounds, also in that row is the number of cases, what I was wanting is the number of cases to change when the customer hits the update button and the new number of cases be saved to the database. the gridview comes back up for the customer to change another row if he likes. I really don't need to compare anything I don't think. just need to know how to change and save the cases when the customer updates the total pounds orders (for that row).</p> <p>Thanks</p> 2010-12-28T01:54:03-05:004227687http://forums.asp.net/p/1636083/4227687.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Hi richard1970,</p> <p>You can place a HiddenField control beside the TextBox of total number of pounds, or number of case. To bind the same fields of TextBox&nbsp;to the Value property of HiddenField. Then in the update event by clicking update button you can access the TextBox text and also the value of HiddenField to compare whether it has been changed or not, then do updating. The HiddenField is invisible, so it won't affect you anything, but you can access it.</p> <p>Thanks,</p> 2010-12-28T05:01:32-05:004228437http://forums.asp.net/p/1636083/4228437.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Thanks, I believe that might work, can you show me how to access the text box in the gridview (since there are several lines) i keep getting error that it is not set to a control.</p> 2010-12-28T14:06:59-05:004228979http://forums.asp.net/p/1636083/4228979.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Hi richard1970,</p> <p>If you want to access the TextBoxes in multiple rows, you can loop the GridView and access it from each row:</p> <p>For Each gvr As GridViewRow In GridView1.Rows<br> &nbsp;Dim tb As TextBox = DirectCast(gvr.FindControl(&quot;TextBox1&quot;), TextBox)<br> Next</p> <p>You can use the same way to access any control from ItemTemplate in GridView.</p> <p>Thanks,</p> 2010-12-29T00:39:52-05:004229846http://forums.asp.net/p/1636083/4229846.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Here is what I tried, but I have an error the operands &quot;&lt;&gt;&quot; and &quot;/&quot; are not defined for controls textbox and label:</p> <pre class="prettyprint">Protected Sub rowUpdateing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) For Each gvr As GridViewRow In grdShoppingCart.Rows Dim totalpounds As TextBox = DirectCast(gvr.FindControl(&quot;TextBox1&quot;), TextBox) Dim changedpounds As TextBox = DirectCast(gvr.FindControl(&quot;LastAmount&quot;), TextBox) Dim lbs As Label = DirectCast(gvr.FindControl(&quot;lblLbs1&quot;), Label) Dim cases As TextBox = DirectCast(gvr.FindControl(&quot;lblTotalQuantity&quot;), TextBox) If totalpounds &lt;&gt; changedpounds Then cases = totalpounds / lbs End if DataBind() Next Thanks End Sub</pre> <p><br> &nbsp;</p> 2010-12-29T12:57:48-05:004230563http://forums.asp.net/p/1636083/4230563.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Hi richard1970,</p> <p>You cannot use TextBox control to do this. You need their texts and convert them into integer or double type for calculation.</p> <p>Something like this:</p> <p>Dim totalpounds As TextBox = DirectCast(gvr.FindControl(&quot;TextBox1&quot;), TextBox)<br> Dim total_p As Integer = Convert.ToInt32(totalpounds.Text)<br> Dim changedpounds As TextBox = DirectCast(gvr.FindControl(&quot;LastAmount&quot;), TextBox)<br> Dim total_c As Integer = Convert.ToInt32(changedpounds.Text)<br> Dim lbs As Integer = Convert.ToInt32(DirectCast(gvr.FindControl(&quot;lblLbs1&quot;), Label).Text)<br> <em>'... other control<br> <br> </em>If total_p &lt;&gt; total_c Then<br> &nbsp;cases = total_p \ lbs<br> End If</p> <p>If you use different data type for total and other values, just change to Convert.othertype() method to cast.</p> <p>Thanks,</p> 2010-12-30T00:25:47-05:004231561http://forums.asp.net/p/1636083/4231561.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Hello Qin Dian Tang</p> <p>I changed it but received this error when I tested:</p> <h2><i>Object reference not set to an instance of an object.</i> </h2> <p><font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "><b>Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. <br> <br> <b>Exception Details: </b>System.NullReferenceException: Object reference not set to an instance of an object.<br> <br> <b>Source Error:</b> <br> <br> <table width="100%" bgcolor="#ffffcc"> <tbody> <tr> <td><pre class="prettyprint"><pre>Line 9: For Each gvr As GridViewRow In grdShoppingCart.Rows</pre> Line 10: Dim totalpounds As TextBox = DirectCast(gvr.FindControl(&quot;TextBox1&quot;), TextBox) Line 11: Dim total_p As Integer = Convert.ToInt32(totalpounds.Text) Line 12: Dim changedpounds As TextBox = DirectCast(gvr.FindControl(&quot;LastAmount&quot;), TextBox) Line 13: Dim total_c As Integer = Convert.ToInt32(changedpounds.Text) <pre></pre></pre></td> </tr> </tbody> </table> <br> <b>Source File: </b>C:\Users\Richard Foster\Documents\Visual Studio 2010\WebSites\ecomerce\ShoppingCart.aspx<b> &nbsp;&nbsp; Line: </b>11 <br> <br> Thank you</font></p> 2010-12-30T13:08:43-05:004232248http://forums.asp.net/p/1636083/4232248.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Hi richard1970,</p> <p>It seems you don't have a TextBox called &quot;TextBox1&quot;. Check the real id of TextBox.</p> <p>Thanks,</p> 2010-12-31T00:08:45-05:004232333http://forums.asp.net/p/1636083/4232333.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Hello Qin Dian Tang,</p> <p>I checked and I do have a TextBox1, see below:&nbsp;</p> <li><span>&lt;/asp:TemplateField&gt; &nbsp;&nbsp;</span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;asp:TemplateField&nbsp;HeaderText=</span><span>&quot;Total&nbsp;Pounds&quot;</span><span>&gt; &nbsp;&nbsp;</span><span></span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&lt;ItemTemplate&gt; &nbsp;&nbsp;</span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;asp:Label&nbsp;ID=</span><span>&quot;Label3&quot;</span><span>&nbsp;runat=</span><span>&quot;server&quot;</span><span>&nbsp;Text=</span><span>'&lt;%#&nbsp;Bind(&quot;TotalLbs&quot;)&nbsp;%&gt;'</span><span>&gt;&lt;/asp:Label&gt; &nbsp;&nbsp;</span><span></span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/ItemTemplate&gt; &nbsp;&nbsp;</span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;EditItemTemplate&gt; &nbsp;&nbsp;</span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;asp:TextBox&nbsp; &nbsp;&nbsp;</span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<u>&nbsp;ID=</u></span><span><u>&quot;TextBox1&quot;</u></span><span>&nbsp; &nbsp;&nbsp;</span><span></span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;runat=</span><span>&quot;server&quot;</span><span>&nbsp; &nbsp;&nbsp;</span><span></span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Text=</span><span>'&lt;%#&nbsp;Bind(&quot;TotalLbs&quot;)&nbsp;%&gt;'</span><span>&nbsp; &nbsp;&nbsp;</span><span></span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Width=</span><span>&quot;50&quot;</span><span>&nbsp; &nbsp;&nbsp;</span><span></span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ForeColor=</span><span>&quot;Red&quot;</span><span>&nbsp; &nbsp;&nbsp;</span><span></span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BackColor=</span><span>&quot;Yellow&quot;</span><span>&nbsp; &nbsp;&nbsp;</span><span></span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;BorderStyle=</span><span>&quot;Inset&quot;</span><span>&gt; &nbsp;&nbsp;</span><span></span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/asp:TextBox&gt; &nbsp;&nbsp;</span> </li><li><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/EditItemTemplate&gt;&nbsp;&nbsp;&nbsp;</span> <p><span>so I am at a loss?</span></p> <p><span>Thanks</span></p> </li>2010-12-31T02:21:07-05:004232462http://forums.asp.net/p/1636083/4232462.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Hi richard1970,</p> <p>The controls you want to access are in EditItemTelplate, so you cannot loop the GridView to get them from every row. That's wrong. You can only access the edit row control:</p> <p>Dim totalpounds As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl(&quot;TextBox1&quot;), TextBox)<br> Dim changedpounds As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl(&quot;LastAmount&quot;), TextBox)<br> Dim lbs As Label = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl(&quot;lblLbs1&quot;), Label)<br> Dim cases As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl(&quot;lblTotalQuantity&quot;), TextBox)<br> If totalpounds.Text &lt;&gt; changedpounds.Text Then<br> &nbsp;cases.Text = Convert.ToInt32(totalpounds.Text) / Convert.ToInt32(lbs.Text)<br> End If</p> <p>Thanks,</p> 2010-12-31T04:40:59-05:004232875http://forums.asp.net/p/1636083/4232875.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Thanks,</p> <p>hopefully this works, e.row does not work with&nbsp;&nbsp;GridViewUpdateEventArgs, do you know if I should changed that to something else?</p> <p><font size="2" face="Consolas"><font size="2" face="Consolas"></p> <p>&nbsp;</p> <pre class="prettyprint">Protected Sub rowUpdateing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Dim totalpounds As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl(&quot;TextBox1&quot;), TextBox) Dim changedpounds As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl(&quot;LastAmount&quot;), TextBox) Dim lbs As Label = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl(&quot;lblLbs1&quot;), Label) Dim cases As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl(&quot;lblTotalQuantity&quot;), TextBox) If totalpounds.Text &lt;&gt; changedpounds.Text Then cases.Text = Convert.ToInt32(totalpounds.Text) / Convert.ToInt32(lbs.Text) End If DataBind() End Sub</pre> <p><br> &nbsp;</p> <p></p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>&nbsp;</p> <p>p</font></font>rotected Sub rowUpdateing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim totalpounds As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl(&quot;TextBox1&quot;), TextBox)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim changedpounds As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl(&quot;LastAmount&quot;), TextBox)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim lbs As Label = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl(&quot;lblLbs1&quot;), Label)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Dim cases As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl(&quot;lblTotalQuantity&quot;), TextBox)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; If totalpounds.Text &lt;&gt; changedpounds.Text Then<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cases.Text = Convert.ToInt32(totalpounds.Text) / Convert.ToInt32(lbs.Text)<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; End If<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataBind()<br> &nbsp;&nbsp;&nbsp; End Sub</p> <p></p> <p><br> </p> 2010-12-31T10:58:46-05:004234741http://forums.asp.net/p/1636083/4234741.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Hi richard1970,</p> <p>Sorry, my mistake. Change &quot;e.Row.RowIndex&quot; to &quot;e.RowIndex&quot; in RowUpdating event.</p> <p>Thanks,</p> 2011-01-03T00:12:30-05:004234827http://forums.asp.net/p/1636083/4234827.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>hello Qin Dian Tang</p> <p>It seems to have worked except that my (totalquantiy) or cases item did not change its number in the gridview or the database (i did add it to my update event). do you know why it may not change the number when I changed the totalpounds?</p> <p>thanks&nbsp;</p> 2011-01-03T02:33:17-05:004234851http://forums.asp.net/p/1636083/4234851.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Hi richard1970,</p> <p>What you did in RowUpdating event is just calculate the result to display in Label. You didn't do anything to data source control or database. If you want to update the data to table, you need to assign the values to the DataSource control you use &quot;srcCart&quot;.</p> <p>Thanks,</p> 2011-01-03T03:02:33-05:004234870http://forums.asp.net/p/1636083/4234870.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Hi</p> <p>but I do not see any result in the label either?</p> <p>thanks&nbsp;</p> 2011-01-03T03:29:17-05:004234884http://forums.asp.net/p/1636083/4234884.aspx/1?Re+Gridview+editing+one+cell+and+updating+based+on+calculationRe: Gridview: editing one cell and updating based on calculation <p>Hi richard1970,</p> <p>I think the way is correct. What you need to do is set the breakpoint in event to see which step gets wrong. </p> <p>Thanks,</p> 2011-01-03T03:42:12-05:00