7 Replies - 4476 Views - Last Post: 02 May 2013 - 12:19 AM Rate Topic: -----

#1 AlphaMama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-April 13

How to format number with comma and 2 decimal places in listview.

Posted 30 April 2013 - 08:53 PM

How can I format number like 1,250.25Attached Image to display in list view in Visual Basic 2012. I have the code for loading the data into listview, but I'm not getting the correct format in Unit Cost column that I want.
Is This A Good Question/Topic? 0
  • +

Replies To: How to format number with comma and 2 decimal places in listview.

#2 vks.gautam1  Icon User is offline

  • D.I.C Regular

Reputation: 17
  • View blog
  • Posts: 317
  • Joined: 21-March 08

Re: How to format number with comma and 2 decimal places in listview.

Posted 30 April 2013 - 11:07 PM

hope so this would help you.
 ListView1.Items.Add(FormatNumber(unitcost, 2, TriState.False, TriState.True, TriState.True))




here is the link for this to understand this in better way.
http://msdn.microsof...t(v=vs.71).aspx

This post has been edited by vks.gautam1: 30 April 2013 - 11:09 PM

Was This Post Helpful? 1
  • +
  • -

#3 AlphaMama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-April 13

Re: How to format number with comma and 2 decimal places in listview.

Posted 01 May 2013 - 04:16 PM

Hi New D.I.C. Head,

Greetings!

I have tried what you have suggested and it throw an error.. Below is my code for the listview.

 'load data into listview
  For i = 0 To TABLE.Rows.Count - 1
       With lvDisplay
          .Items.Add(TABLE.Rows(i)("ID").ToString)
           With .Items(.Items.Count - 1).SubItems
               .Add(TABLE.Rows(i)("tDescription").ToString)
               .Add(TABLE.Rows(i)(FormatNumber("tunitcost", 2, TriState.False, TriState.True, TriState.True)))
               .Add(TABLE.Rows(i)(String.Format("tDate", "MM/dd/yyyy")))
           End With
        End With
        If i Mod 2 Then
            lvDisplay.Items(i).BackColor = Color.White
        Else
            lvDisplay.Items(i).BackColor = Color.WhiteSmoke
        End If
   Next


I have tried the below commands but failed too.
   '.Add(TABLE.Rows(i)(String.Format("{0:0,000.00}", "tUnitCost")))
   '.Add(TABLE.Rows(i)("tUnitCost").ToString("{0:0,000.00}"))



*** EDIT ***
Please use code tags when posting code
:code:

This post has been edited by GunnerInc: 01 May 2013 - 05:50 PM
Reason for edit:: Removed quote and added code tags

Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: How to format number with comma and 2 decimal places in listview.

Posted 01 May 2013 - 05:22 PM

.Add(TABLE.Rows(i)(FormatNumber("tunitcost", 2, TriState.False, TriState.True, TriState.True)))


FormatNumber takes an object that evaluates to a number. You gave it a string with the content of "tunitcost".
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How to format number with comma and 2 decimal places in listview.

Posted 01 May 2013 - 06:04 PM

how about
.Add(Cdbl(TABLE.Rows(i)("tUnitCost")).ToString("#,###.00"))


Haven't tried it but as long as the value can be cast to a double, the toString method should format it as displayed.

This post has been edited by CharlieMay: 01 May 2013 - 06:08 PM

Was This Post Helpful? 0
  • +
  • -

#6 AlphaMama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-April 13

Re: How to format number with comma and 2 decimal places in listview.

Posted 01 May 2013 - 08:43 PM

Hi CharlieMay,

The same result as above, the Unit Cost is not formatted in listview.
.Add(Cdbl(TABLE.Rows(i)("tUnitCost")).ToString("#,###.00"))

Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: How to format number with comma and 2 decimal places in listview.

Posted 01 May 2013 - 11:23 PM

Well, my problem is that I know next to nothing about database operations, so I am going to make the assumption that:

.Add(TABLE.Rows(i)("tUnitCost"))

will add the appropriate number to the ListBox, but in the wrong format.

So, extrapolating from that, I think you should try:

.Add(FormatNumber(TABLE.Rows(i)("tunitcost"), 2, TriState.False, TriState.True, TriState.True))

Was This Post Helpful? 1
  • +
  • -

#8 AlphaMama  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 30-April 13

Re: How to format number with comma and 2 decimal places in listview.

Posted 02 May 2013 - 12:19 AM

Yes! That works perfectly. Thank you lar3ry, it's a big help for my project.
.Add(FormatNumber(TABLE.Rows(i)("tunitcost"), 2, TriState.False, TriState.True, TriState.True))

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1