Home/ASP.NET Forums/General ASP.NET/Web Forms Data Controls/Gridview: editing one cell and updating based on calculation

Gridview: editing one cell and updating based on calculation [Answered]RSS

26 replies

Last post Jan 04, 2011 04:06 PM by richard1970

Richard
  • Qin Dian Tang - MSFT

    Qin Dian Tan...

    All-Star

    98031 Points

    12426 Posts

    Microsoft

    Re: Gridview: editing one cell and updating based on calculation

    Dec 31, 2010 05:40 AM|Qin Dian Tang - MSFT|LINK

    Hi richard1970,

    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:

    Dim totalpounds As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl("TextBox1"), TextBox)
    Dim changedpounds As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl("LastAmount"), TextBox)
    Dim lbs As Label = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl("lblLbs1"), Label)
    Dim cases As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl("lblTotalQuantity"), TextBox)
    If totalpounds.Text <> changedpounds.Text Then
     cases.Text = Convert.ToInt32(totalpounds.Text) / Convert.ToInt32(lbs.Text)
    End If

    Thanks,

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
    Microsoft One Code Framework
  • richard1970

    richard1970

    Member

    5 Points

    24 Posts

    Re: Gridview: editing one cell and updating based on calculation

    Dec 31, 2010 11:58 AM|richard1970|LINK

    Thanks,

    hopefully this works, e.row does not work with  GridViewUpdateEventArgs, do you know if I should changed that to something else?

     

    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("TextBox1"), TextBox)
            Dim changedpounds As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl("LastAmount"), TextBox)
            Dim lbs As Label = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl("lblLbs1"), Label)
            Dim cases As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl("lblTotalQuantity"), TextBox)
            If totalpounds.Text <> changedpounds.Text Then
                cases.Text = Convert.ToInt32(totalpounds.Text) / Convert.ToInt32(lbs.Text)
            End If
            DataBind()
        End Sub


     

     

     

     

     

     

     

     

     

     

     

     

    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("TextBox1"), TextBox)
            Dim changedpounds As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl("LastAmount"), TextBox)
            Dim lbs As Label = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl("lblLbs1"), Label)
            Dim cases As TextBox = DirectCast(grdShoppingCart.Rows(e.Row.RowIndex).FindControl("lblTotalQuantity"), TextBox)
            If totalpounds.Text <> changedpounds.Text Then
                cases.Text = Convert.ToInt32(totalpounds.Text) / Convert.ToInt32(lbs.Text)
            End If
            DataBind()
        End Sub


    Richard
  • Qin Dian Tang - MSFT

    Qin Dian Tan...

    All-Star

    98031 Points

    12426 Posts

    Microsoft

    Re: Gridview: editing one cell and updating based on calculation

    Jan 03, 2011 01:12 AM|Qin Dian Tang - MSFT|LINK

    Hi richard1970,

    Sorry, my mistake. Change "e.Row.RowIndex" to "e.RowIndex" in RowUpdating event.

    Thanks,

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
    Microsoft One Code Framework
  • richard1970

    richard1970

    Member

    5 Points

    24 Posts

    Re: Gridview: editing one cell and updating based on calculation

    Jan 03, 2011 03:33 AM|richard1970|LINK

    hello Qin Dian Tang

    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?

    thanks 

    Richard
  • Qin Dian Tang - MSFT

    Qin Dian Tan...

    All-Star

    98031 Points

    12426 Posts

    Microsoft

    Re: Gridview: editing one cell and updating based on calculation

    Jan 03, 2011 04:02 AM|Qin Dian Tang - MSFT|LINK

    Hi richard1970,

    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 "srcCart".

    Thanks,

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
    Microsoft One Code Framework
  • richard1970

    richard1970

    Member

    5 Points

    24 Posts

    Re: Gridview: editing one cell and updating based on calculation

    Jan 03, 2011 04:29 AM|richard1970|LINK

    Hi

    but I do not see any result in the label either?

    thanks 

    Richard
  • Qin Dian Tang - MSFT

    Qin Dian Tan...

    All-Star

    98031 Points

    12426 Posts

    Microsoft

    Re: Gridview: editing one cell and updating based on calculation

    Jan 03, 2011 04:42 AM|Qin Dian Tang - MSFT|LINK

    Hi richard1970,

    I think the way is correct. What you need to do is set the breakpoint in event to see which step gets wrong.

    Thanks,

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
    Microsoft One Code Framework
  • richard1970

    richard1970

    Member

    5 Points

    24 Posts

    Re: Gridview: editing one cell and updating based on calculation

    Jan 03, 2011 05:10 AM|richard1970|LINK

    I changed the code to reflect this. then ran breakpoint all variables are correct to what I changed them to and the cases.text is correct for the updated number, but the label in gridview still shows old number.

    Protected Sub rowUpdateing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)
            Dim totalpounds As TextBox = DirectCast(grdShoppingCart.Rows(e.RowIndex).FindControl("TextBox1"), TextBox)
            Dim lbs As Label = DirectCast(grdShoppingCart.Rows(e.RowIndex).FindControl("lblLbs1"), Label)
            Dim cases As TextBox = DirectCast(grdShoppingCart.Rows(e.RowIndex).FindControl("lblTotalQuantity"), TextBox)
            Dim changecases As Integer
            changecases = Convert.ToInt32(totalpounds.Text) / Convert.ToInt32(lbs.Text)
            cases.Text = changecases
                
    
            
    
        End Sub


    thanks 

    Richard
  • Qin Dian Tang - MSFT

    Qin Dian Tan...

    All-Star

    98031 Points

    12426 Posts

    Microsoft

    Re: Gridview: editing one cell and updating based on calculation

    Jan 03, 2011 06:35 AM|Qin Dian Tang - MSFT|LINK

    Hi richard1970,

    After you click the update button and fires the updating event, is it still in edit mode of row, or changed to readonly mode? If you just want to only calculate the data, you don't have to handle the rowupdating event. You can handle rowcommand or even use javascript to do the calculation.

    Thanks,

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
    Microsoft One Code Framework
  • richard1970

    richard1970

    Member

    5 Points

    24 Posts

    Re: Gridview: editing one cell and updating based on calculation

    Jan 03, 2011 04:13 PM|richard1970|LINK

    HI

    after I hit the update button the update event fires updating the information in the table, it is changed to read only then as you asked. I do need the database to update with the new data however, but the update is grabing the numbers that are shown in the table. if I could change the numbers before the update happens is what I need.

    thanks 

    Richard
  • Qin Dian Tang - MSFT

    Qin Dian Tan...

    All-Star

    98031 Points

    12426 Posts

    Microsoft

    Re: Gridview: editing one cell and updating based on calculation

    Jan 04, 2011 03:04 AM|Qin Dian Tang - MSFT|LINK

    Hi richard1970, in

    I guess you need to update the text of Label "lblTotalQuantity", right? First change Bind("TotalQuantity") to Eval("TotalQuantity") for Label "lbl3" in ItemTemplate. Then in RowUpdating event you need to assign the value after calculated to UpdateParameters manually. I mean you can set the value of "changecases" to the update parameter of "TotalQuantity" field in DataSource control. Another thing you need to check is in markup I see the "lblTotalQuantity" is a Label, but in RowUpdating event you cast it as a TextBox.

    Thanks,

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
    Microsoft One Code Framework
  • richard1970

    richard1970

    Member

    5 Points

    24 Posts

    Re: Gridview: editing one cell and updating based on calculation

    Jan 04, 2011 05:25 AM|richard1970|LINK

    Yes, I need to update the label, I have changed the bind to eval. Ok, I am at a loss on the manual update parameters. I have an object data source below is the update method called in the code behind.

    Public Shared Sub Update(ByVal id As Integer, ByVal totallbs As Integer, ByVal totalquantity As Integer)
                ' Initialize command
                Dim con As New SqlConnection(_connectionString)
                Dim cmd As New SqlCommand("UPDATE ShoppingCarts SET TotalLbs =  @TotalLbs, TotalQuantity = @TotalQuantity WHERE Id=@Id", con)
                cmd.Parameters.Add(New SqlParameter("@Id", id))
                cmd.Parameters.Add(New SqlParameter("@TotalLbs", totallbs))
                cmd.Parameters.Add(New SqlParameter("@TotalQuantity", totalquantity))
                con.Open()
                cmd.ExecuteNonQuery()
                con.Close()
    
    
                ' Clear session cart
                Dim context As HttpContext = HttpContext.Current
                context.Session.Remove("ShoppingCart")
            End Sub


     

    How do I manually update TotalQuantity from the value changecases from the page to this method?

    Thank you, and I want to say I appriciate everything you have taught me here also.

    Richard
  • Qin Dian Tang - MSFT

    Qin Dian Tan...

    All-Star

    98031 Points

    12426 Posts

    Microsoft

    Re: Gridview: editing one cell and updating based on calculation

    Jan 04, 2011 07:13 AM|Qin Dian Tang - MSFT|LINK

    Hi richard1970,

    You need to add UpdateParameters into ObjectDataSource for the "TotalQuantity". Then in RowUpdating event you can assign value to it:

    srcCart.UpdateParameters("TotalQuantity").DefaultValue = totalpounds.Text

    Thanks,

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
    Microsoft One Code Framework
  • richard1970

    richard1970

    Member

    5 Points

    24 Posts

    Re: Gridview: editing one cell and updating based on calculation

    Jan 04, 2011 04:06 PM|richard1970|LINK

    Hello Qin Dian Tang

    It worked, thank you so much.  I did have to take the edit item templete out of the cases column (totalquantity) and once I did that and followed your directions above it works

    Thanks! 

    Richard
  • ‹ Previous Thread|Next Thread ›