Results 1 to 2 of 2

Thread: [2005] Computing Total value Based on Content in two Columns

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2007
    Posts
    134

    Question [2005] Computing Total value Based on Content in two Columns

    Hi,

    I have two columns in two different DataGridViews that I would like to Sum up to arrive at the value that is to show up in my WORKORDERTOTAL textbox, what I have now does not work at all. How do I best calculate this sort of expression ?

    2) Others have to display other values but What I notice is that I need to actually click on the textboxes for the computation to show up. Is there a better way to handle this please?

    Code:
           txtQtyTotal.Text = (DS.Tables("LineItems").Compute("Sum(QUANTITYRECORDED)", "QUANTITYRECORDED <= 10000000")).ToString
            txtTotals.Text = (DS.Tables("LineItems").Compute("Sum(TOTALCOST)", "TOTALCOST <= 10000000")).ToString
    
            txtWORKORDERTOTAL.Text = (DS.Tables("LineItems").Compute("Sum(TOTALCOST)", "TOTALCOST <= 10000000")).ToString
            txtWORKORDERTOTAL.Text += (DS.Tables("PackedLineItems").Compute("Sum(TOTALCOST)", "TOTALCOST <= 10000000")).ToString

    thanks in Advance

    Reaction

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Computing Total value Based on Content in two Columns

    Look at this code:
    vb.net Code:
    1. txtWORKORDERTOTAL.Text += (DS.Tables("PackedLineItems").Compute("Sum(TOTALCOST)", "TOTALCOST <= 10000000")).ToString
    Both sides of the '+=' operator are String objects. What happens when you "add" two strings? They are concatenated into one longer string. If you want to perform mathematical addition on two numbers then you have to use numbers, not strings. The Compute method will return an Object reference that will contain a value of the same type as the column you're aggregating. If the TotalCost column is type Integer then Compute will return an Integer. Likewise for Double, Decimal etc. You should be casting the result of the Compute method as the appropriate type using Cint, CDbl or whatever. THEN you will have number sthat you can add together.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width