Results 1 to 8 of 8

Thread: DataGrid columns math

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    123

    DataGrid columns math

    I have a table in a data grid the columns are bound to the columns...

    SQFT QTY WIDTH LENGTH

    i also have added into the datagrid an unbound column called Yeild%

    what i'm trying to do is the following formula

    SQFT / (QTY * (width * length / 144) and have the result return the answer in the unbound yield column for that row represented as a percentage

    So if SQFT = 178 QTY = 2 Width = 144 Length = 102 answer would be 0.87 (rounded)

    so this would be 87%

    Also if there's a better method any help would be appreciated

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

    Re: DataGrid columns math

    A better option would be to add another DataColumn to your DataTable after populating it and setting its Expression property. The values will then be calculated automatically with no need for code at all.
    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

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    123

    Re: DataGrid columns math

    So the unbound data column i added called value% could i use this column for the expression? And could you just give me a very basic walkthrough of a simple expression, just enough so i can figure out the rest.

    edit:: ok i see where i do this in the dataset designer, so just a couple of examples of how the expression syntax works as i've never used this feature before
    Last edited by addntox; Sep 20th, 2010 at 10:24 PM.

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

    Re: DataGrid columns math

    That's exactly what the documentation is for. Open the MSDN Library and look up DataColumn.Expression. There's your examples and more besides.
    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

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    123

    Re: DataGrid columns math

    ok sir i got the expression working, one thing though in MSDN syntax i dont see a round function, any tips on rounding the expression?

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

    Re: DataGrid columns math

    The options available to you are limited. You could try the CONVERT function to see if it will convert your Double to an Int32. If it does, just check to see whether it rounds or truncates. If it truncates then you could just add 0.5 to everything before converting.
    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    123

    Re: DataGrid columns math

    Yea it truncates, just thinking out loud here but if i set the expression to only run if the column row value is null (so as a new row is added) couldnt i pass the columns through a math.round function on that datagridview (i'm using parent/child tables using one to many) when saving so it writes back the rounded value to the sql server?...if ya get my drift

    ok forget that, expression is read only
    Last edited by addntox; Sep 21st, 2010 at 12:26 AM.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2009
    Posts
    123

    Re: DataGrid columns math

    ok well here's how i kinda did it.

    I re did the test table and added a second column called total2 to use as my expression and removed it from view so as the table updates it runs through.
    vb Code:
    1. Dim cr As Decimal
    2. For count As Integer = 0 To TestDataGridView.Rows.Count - 1
    3. Dim currentcell As DataGridViewCell = TestDataGridView.Rows(count).Cells("total2")
    4.  Dim currentcell2 As DataGridViewCell = TestDataGridView.Rows(count).Cells("total")
    5. cr = Decimal.Round(currentcell.Value, 2)
    6.  currentcell2.Value = cr
    Last edited by addntox; Sep 21st, 2010 at 01:13 AM.

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