We help IT Professionals succeed at work.

Format Textbox for currency.

mindserve
mindserve used Ask the Experts™
on
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")
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shahan AyyubSenior Software Engineer

Commented:
       
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("##.##")

Author

Commented:
Problem is not to trim..I only wish that would be the problem.
I get 45.00 not 45.50

Author

Commented:
If I could get $455.569 then I could truncate but that's not happening here...
Shahan AyyubSenior Software Engineer

Commented:
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("##.##")
Senior Software Engineer
Commented:
Did you get what I said ??

Author

Commented:
"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

Author

Commented:
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.