Link to home
Start Free TrialLog in
Avatar of mindserve
mindserve

asked on

Format Textbox for currency.

I can't get the txtbox to format correctly. It shows $ but if the result of the sum total on the datagrid is $45.50 I only see $4.00 in the textbox.

 For Each dr As DataGridViewRow In DataGridView1.Rows
            Dim c As DataGridViewCheckBoxCell = CType(dr.Cells(0), DataGridViewCheckBoxCell)
            If ((Not (c) Is Nothing) _
                        AndAlso CType(c.FormattedValue, Boolean)) Then
                sum = (sum + Integer.Parse(dr.Cells(2).Value.ToString(0.0)))          
 End If
        Next
   i = (i + 1)
  txttbl.Text = sum.ToString("c")
 txttbl.Text = CDec(txttbl.Text).ToString("c")
Avatar of Shahan Ayyub
Shahan Ayyub
Flag of Pakistan image

       
you should do like this:

 For Each dr As  DataGridViewRow In DataGridView1.Rows
            Dim c As  DataGridViewCheckBoxCell = CType(dr.Cells(0), DataGridViewCheckBoxCell)
             If ((Not (c) Is Nothing) _
                        AndAlso  CType(c.FormattedValue, Boolean)) Then
                sum = (sum +  Integer.Parse(dr.Cells(2).Value.ToString(0.0)))            
 End If
        Next
   i = (i + 1)

        sum = sum.TrimStart("$")   ' assuming sum = "$455.569"
        txttbl.Text = "$" & CDec(sum).ToString("##.##")
Avatar of mindserve
mindserve

ASKER

Problem is not to trim..I only wish that would be the problem.
I get 45.00 not 45.50
If I could get $455.569 then I could truncate but that's not happening here...
this line does formatting:

txttbl.Text = "$" & CDec(sum).ToString("##.##")

and at this line "$" should not be the part of 'sum'. If you had already removed it then remove the bold line:

For Each dr As  DataGridViewRow In DataGridView1.Rows
             Dim c As  DataGridViewCheckBoxCell = CType(dr.Cells(0),  DataGridViewCheckBoxCell)
             If ((Not (c) Is Nothing) _
                         AndAlso  CType(c.FormattedValue, Boolean)) Then
                 sum = (sum +  Integer.Parse(dr.Cells(2).Value.ToString(0.0)))            
 End If
        Next
   i = (i + 1)

        sum = sum.TrimStart("$")   ' assuming sum = "$455.569"
         txttbl.Text = "$" & CDec(sum).ToString("##.##")
ASKER CERTIFIED SOLUTION
Avatar of Shahan Ayyub
Shahan Ayyub
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
"assuming sum" but ...it is not $455.569
It is supposed to be $45.50 and it's $45.00
Its not showing the .50
For Each dr As DataGridViewRow In DataGridView1.Rows
            Dim c As DataGridViewCheckBoxCell = CType(dr.Cells(0), DataGridViewCheckBoxCell)
            If ((Not (c) Is Nothing) _
                        AndAlso CType(c.FormattedValue, Boolean)) Then
                sum = (sum + Decimal.Parse(dr.Cells(2).Value))            
End If
        Next
        i = (i + 1)
             txttbl.Text = CDec(sum).ToString("##.##")

I changed Integer to Decimal. parse and that seems to work better.