none
Formatting of Data in Listview

    Question

  • I have a listview which is populated from a Table. My code is as under:
    'Transfer Data from Table to ListView
            Dim i As Integer
            For i = 0 To ds.Tables(0).Rows.Count - 1
                Dim drow As DataRow = ds.Tables(0).Rows(i)
                Dim lvi As ListViewItem = New ListViewItem(drow("Description").ToString)
                lvi.SubItems.Add(drow("Quantity").ToString)
                lvi.SubItems.Add(drow("Price").ToString)
                lvi.SubItems.Add(drow("Amount").ToString)
                ListView1.Items.Add(lvi)
            Next
    How I can format data of Listview for Price and Amount like  $40.00. because the listview shows data as 40 only instead of $40.00 as it is displayed in Table.
    Please advise.
    Sunday, August 09, 2009 3:10 AM

Answers

All replies

  • Hi,
    I have not tried as a complete solution but you may want to try this:

                lvi.SubItems.Add(format((drow("Price").ToString),"C"))

    HTH - if so mark this as an answer so that this can be closed - happy programming.
    • Proposed as answer by Cor LigthertMVP Sunday, August 09, 2009 5:27 AM
    • Unproposed as answer by sazd1 Wednesday, August 12, 2009 12:10 PM
    Sunday, August 09, 2009 4:50 AM
  • Hi Novoice
    Thanks for your reply.
    I tried your code but it is displaying the word C in the price column instead of actual price in currency format.
    Please advise.

    Monday, August 10, 2009 11:45 AM
  • Hi sazd1,

       I think you can try this:
       lvi.SubItems.Add(String.Format("{0:C}", drow("Price"))
      "0" is on behalf of the first argument, "C" is the currency format. 
      You may refer to the following link for more information
      http://msdn.microsoft.com/en-us/library/system.string.format.aspx

    Hope this helps

    Regards
    Jeff Shan
    • Marked as answer by sazd1 Wednesday, August 12, 2009 12:11 PM
    Tuesday, August 11, 2009 6:57 AM
  • Hi Jeff

    Thanks for your reply. yes it is working but it is showing the figure of 18.50 as 18... ie. 18 with three dots only.
    Please guide what is wrong.
    Like i have figures of price.............. 2.70    18.50      26.90
    Thanks once again.
    Tuesday, August 11, 2009 2:02 PM
  • Hi sazd1,
       
        This line works well on my computer, i think may be it is a culture problem.
        Would you please change the culture of your application to "en-US" to see whether the issue still there?
        My.Application.ChangeCulture("en-US")
        Good luck!

    Regards
    Jeff Shan
    Wednesday, August 12, 2009 4:58 AM
  • Hi Jeff

    Thanks for your reply. yes it worked now. Infact the width of price column was not enough to accomodate 18.50 so when i increased the width of Price column in Listview it became OK.
    Thanks so much for all your guidance and help. So this is the line which did the magic.

    ls.SubItems.Add(

    String.Format("{0:C}", dr("Price")))

    I am going to mark your reply as Answer.
    Just one more query if i want to disply price only as 5.50 or 18.50 without currency sign, so i will have to use "0.00" inplace of 0:C or something else. Please advise.
    Thanks once again.

    Wednesday, August 12, 2009 12:10 PM
  • Try something like:

        lvi.SubItems.Add(drow("Price").ToString("0.00"))

    Rod

    Rod Stephens, Visual Basic MVP

    Beginning Database Design Solutions
    http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/

    Visual Basic 2008 Programmer's Reference
    http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
    Wednesday, August 12, 2009 12:21 PM
  • Hi Stephens 
    I tried that but what it is doing that it is showing only one digit and skipping all the rest of digits.
    Like for 5.40 it is showing 5 only........for 18.50 it is showing 1 only....for 32.00 it is showing 3 only.
    This is a strange behaviour. Please advise what is wrong with this. I am copying my code here.
    While dr.Read
                            Dim ls As New ListViewItem(dr.Item("DTDate").ToString())
                            ls.SubItems.Add(dr.Item("DTTime").ToString())
                            ls.SubItems.Add(dr.Item("CustId").ToString())
                            ls.SubItems.Add(dr.Item("Description").ToString())
                            ls.SubItems.Add(dr.Item("Quantity").ToString())
                            ls.SubItems.Add(dr.Item("Price").ToString("0.00"))
                           ls.SubItems.Add(dr.Item("Amount").ToString("0.00"))
                           ListView1.Items.Add(ls)
                        End While




    Thursday, August 13, 2009 11:30 AM
  • Hi
    I tried something like Jeff guided and it worked OK. So to display numbers in Listview from Table the code is as under:

    ls.SubItems.Add(

    String.Format("{0:N2}", dr("Amount")))

    Thanks for the guidance of all.

    Thursday, August 13, 2009 11:53 AM
  • Strange. It seems to work for me.

    But if you got another solution working, no problem.

    Rod

    Rod Stephens, Visual Basic MVP

    Beginning Database Design Solutions
    http://www.amazon.com/exec/obidos/ASIN/0470385499/vbhelper/

    Visual Basic 2008 Programmer's Reference
    http://www.amazon.com/exec/obidos/ASIN/0470182628/vbhelper/
    Thursday, August 13, 2009 1:15 PM
  • Hi Stephens

    Thanks Stephens. You have guided me on so many points and so helpful in solving my issues in develpment.
    Thanks and hope for your cooperation in future too.
    Friday, August 14, 2009 11:11 AM