Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone

I am trying to insert rows into a listview where most of the items are numeric.

VB
Private Sub but_Accept_Click(sender As System.Object, e As System.EventArgs) Handles but_Accept.Click
    Dim arr(13) As String

    arr(0) = cbo_Vehicle.Text
    arr(1) = String.Format("{0:n2}", txt_Fuel.Text)
    arr(2) = String.Format("{0:n2}", txt_Service.Text)
    arr(3) = String.Format("{0:n2}", txt_Oil.Text)
    arr(4) = String.Format("{0:n2}", txt_Toll.Text)
    arr(5) = String.Format("{0:n2}", txt_eToll.Text)
    arr(6) = String.Format("{0:n2}", txt_Tyres.Text)
    arr(7) = String.Format("{0:n2}", txt_Repairs.Text)
    arr(8) = String.Format("{0:n2}", txt_License.Text)
    arr(9) = String.Format("{0:n2}", txt_Crane.Text)
    arr(10) = String.Format("{0:n2}", txt_Tracker.Text)
    arr(11) = String.Format("{0:n2}", txt_Insurance.Text)
    arr(12) = String.Format("{0:n2}", txt_Depreciation.Text)

    lsv_Financial.Items.Add(New ListViewItem(arr))

End Sub


The value 11, 11.1 or 11.11 could be typed into, lets say, txt_fuel (Textbox).
When I add the array to the listview I get the exact copy of what was entered in the textbox instead of 11.00 or 11.10 (the only correct result is when 11.11 was entered)

I have tried a few different .Format statements but always with the same result.

Can you please advise how I can display decimalised values in the listview.

Thanks.
Posted

1 solution

It's beacuse you're passing string to String.Format. Try to convert string from textboxes to i.e. decimal or float:

arr(1) = String.Format("{0:n2}", decimal.Parse(txt_Fuel.Text))

And don't forget to check if textbox contains proper numeric value. Use Decimal.TryParse first.
 
Share this answer
 
v2
Comments
Darrell de Wet 10-Mar-14 8:16am    
Ah, you are a star - thanks very much.
Marcin Kozub 10-Mar-14 8:17am    
Glad I could help you :)

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