Click here to Skip to main content
15,891,748 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
For my column Total i want to add up the numbers in my column. So say there are 10 people and only 8 inputed data...then it would be 8/10 * 100..... then i want to divide the number >=14 by the number inputed which would be 8 people

Dim ccount22 = 0
        Dim ccount55 = 0
        For Each b As DataRow In myTable.Rows
            If b.Item("TI (Y/N)").ToString.ToUpper = "Y" Then ccount22 += 1
            If b.Item("TI (Y/N)").ToString.ToUpper = "N" Then ccount55 += 0
        Next
        Me.txtTI.Text = Math.Round((ccount22 / (count)) * 100, 2).ToString() & "%"
Posted
Comments
Sergey Alexandrovich Kryukov 29-Aug-13 11:19am    
Column of what?
—SA

1 solution

The problem is two fold: First, you don't appear to set a value of count so there is no guarantee that it is anything like what you want - I would suggest that you should set it to myTable.Rows.Count, or set it to zero and increment it in the loop.

More importantly, you are doing integer arithmetic! So if ccount22 is 8, and count is 10, then
VB
count22 / count
Will be
8 / 10
which in integer arithmetic is zero.
There are two things you can do here - swap to explicit float or double, or ahcnge the calculation to make it work with integers:
VB
Me.txtTI.Text = ((ccount22 * 100) / count00).ToString() & "%"
Because you do the multiply first, you end up with
800 / 10
Which is 80, and correct.

BTW: You do realize that your second line within the loop does absolutely nothing useful, don't you? :laugh:
VB
If b.Item("TI (Y/N)").ToString.ToUpper = "N" Then ccount55 += 0
Try:
VB
If b.Item("TI (Y/N)").ToString.ToUpper = "N" Then ccount55 += 1
Instead...

BTW2: That isn't a "nice" way to do it anyway; this is easier to read and a lot more efficient:
VB
For Each b As DataRow In myTable.Rows
    Dim YesNo as string = b.Item("TI (Y/N)").ToString.ToUpper
    If YesNo = "Y" Then ccount22 += 1
    If YesNo = "N" Then ccount55 += 0
Next
 
Share this answer
 
Comments
PythonProgrammer 29-Aug-13 11:50am    
woops i gave u my wrong code above here it is..

Dim ccount3 = 0
Dim ccount4 = 0
'For Each c As DataRow In myTable.Rows
If c.Item("Total").ToString.ToUpper >= "14" Then ccount3 += 1
If c.Item("Total").ToString.ToUpper <= "13" Then ccount4 += 1
Next
Me.txtMR.Text = Math.Round((ccount3 / (ccount4)) * 100, 2).ToString() & "%"
OriginalGriff 29-Aug-13 12:01pm    
Same problem, same solution - and it isn't a good idea to compare strings for greater-than or less-than. The comparison is a string comparison, which means it doesn't work the way you think it does, because it is evaluated on a character-by-character basis, rather than as a numeric value. That means that the order does not ascend as you would expect:
1
10
11
12
13
14
15
16
17
18
19
2
20
21
22
...
Nelek 29-Aug-13 13:09pm    
Tell me. I have to deal with such crap every day. Thats the "Symbol" sorting of the software I use to programm GUIs
OriginalGriff 29-Aug-13 14:04pm    
:doh:

Defenestration for the originator may seem cruel, but it's a good solution in the long run!
PythonProgrammer 29-Aug-13 12:03pm    
how to i change the ToString then?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900