|
-
Sep 20th, 2010, 10:10 PM
#1
Thread Starter
Lively Member
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
-
Sep 20th, 2010, 10:16 PM
#2
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.
-
Sep 20th, 2010, 10:18 PM
#3
Thread Starter
Lively Member
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.
-
Sep 20th, 2010, 10:31 PM
#4
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.
-
Sep 20th, 2010, 11:07 PM
#5
Thread Starter
Lively Member
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?
-
Sep 20th, 2010, 11:15 PM
#6
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.
-
Sep 20th, 2010, 11:41 PM
#7
Thread Starter
Lively Member
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.
-
Sep 21st, 2010, 12:49 AM
#8
Thread Starter
Lively Member
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:
Dim cr As Decimal For count As Integer = 0 To TestDataGridView.Rows.Count - 1 Dim currentcell As DataGridViewCell = TestDataGridView.Rows(count).Cells("total2") Dim currentcell2 As DataGridViewCell = TestDataGridView.Rows(count).Cells("total") cr = Decimal.Round(currentcell.Value, 2) 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|