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.ToSt ring(0.0)) )
End If
Next
i = (i + 1)
txttbl.Text = sum.ToString("c")
txttbl.Text = CDec(txttbl.Text).ToString ("c")
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).
End If
Next
i = (i + 1)
txttbl.Text = sum.ToString("c")
txttbl.Text = CDec(txttbl.Text).ToString
ASKER
Problem is not to trim..I only wish that would be the problem.
I get 45.00 not 45.50
I get 45.00 not 45.50
ASKER
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.ToSt ring(0.0)) )
End If
Next
i = (i + 1)
sum = sum.TrimStart("$") ' assuming sum = "$455.569"
txttbl.Text = "$" & CDec(sum).ToString("##.##" )
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).
End If
Next
i = (i + 1)
sum = sum.TrimStart("$") ' assuming sum = "$455.569"
txttbl.Text = "$" & CDec(sum).ToString("##.##"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
"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
It is supposed to be $45.50 and it's $45.00
Its not showing the .50
ASKER
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.
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).
End If
Next
i = (i + 1)
txttbl.Text = CDec(sum).ToString("##.##"
I changed Integer to Decimal. parse and that seems to work better.
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).
End If
Next
i = (i + 1)
sum = sum.TrimStart("$") ' assuming sum = "$455.569"
txttbl.Text = "$" & CDec(sum).ToString("##.##"