Results 1 to 11 of 11

Thread: [RESOLVED] Listview Columkn Format

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Resolved [RESOLVED] Listview Columkn Format

    I am trying to import from a DGV to a list view and have a column or 2 formatted as currency...
    My current code is
    Code:
                If m_row.Cells("clmItem").Value IsNot Nothing Then
                    Dim item As String = m_row.Cells("clmItem").Value.ToString
                    Dim brand As String = m_row.Cells("clmBrand").Value.ToString
                    Dim section As String = m_row.Cells("clmSection").Value.ToString
                    Dim size As String = m_row.Cells("clmQTY").Value.ToString
                    Dim store As String = m_row.Cells("clmStore").Value.ToString
                    Dim price As String = m_row.Cells("clmPrice").Value.ToString("{0:C}")
                    Dim coupon As String = m_row.Cells("clmCoupon").Value.ToString("{0:C}")
                    Dim lvItem As New ListViewItem(New String() {item, brand, section, size, store, price, coupon})
    
                    PriceMatchStore.Shopping_List.Items.Add(lvItem)
                    PriceMatchStore.Shopping_List.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
                    PriceMatchStore.Shopping_List.AutoResizeColumn(3, ColumnHeaderAutoResizeStyle.HeaderSize)
                    'PriceMatchStore.Shopping_List.AutoResizeColumn(5, ColumnHeaderAutoResizeStyle.HeaderSize)
                End If
    so the Value.ToString("{0:C}") is not working.....
    Also i need to delete the cells that end up showing 0 from these 2 columns but I havent figured out how to do that either. Any help would be appreciated.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Listview Columkn Format

    You're trying to combine two methods of formatting there. "c" is the format specifier for currency. You only use braced place-holders, e.g. {0} when performing composite formatting, e.g. using String.Format. So, this:
    Code:
    String.Format("{0:c}", value)
    is equivalent to this:
    Code:
    value.ToString("c")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Re: Listview Columkn Format

    So what should I have used?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Listview Columkn Format

    Um, you should have used one of the two options that I showed in my previous post. When all you want is the String version of one value then calling ToString is the logical option. String.Format should be used when you want to combine that String value with other String values, literal or otherwise.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Re: Listview Columkn Format

    ok so I am getting this error. ( I actually tried this way before and it didnt work. )
    Attachment 96815

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Listview Columkn Format

    Yes that because you're essentially converting a string to a string. The Value of a dgv cell is Text. You can't format a string to currency, only a number so ...

    Dim c = CDec(dgv.Rows(0).Cells("Column1").Value).ToString("c")
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Re: Listview Columkn Format

    ok so this is my code
    Code:
            PriceMatchStore.Shopping_List.View = View.Details
            PriceMatchStore.Shopping_List.Columns.AddRange(New ColumnHeader() _
                                      {New ColumnHeader With {.Name = "Item", .Text = "Item"}, _
                                       New ColumnHeader With {.Name = "Brand", .Text = "Brand"}, _
                                       New ColumnHeader With {.Name = "Section", .Text = "Section"}, _
                                       New ColumnHeader With {.Name = "Size", .Text = "Size"}, _
                                       New ColumnHeader With {.Name = "Store", .Text = "Store"}, _
                                       New ColumnHeader With {.Name = "Price", .Text = "Price"}, _
                                       New ColumnHeader With {.Name = "Coupon", .Text = "Coupon"}})
            For Each m_row As System.Windows.Forms.DataGridViewRow In Me.ShoppingListView.Rows
                If m_row.Cells("clmItem").Value IsNot Nothing Then
                    Dim item As String = m_row.Cells("clmItem").Value.ToString
                    Dim brand As String = m_row.Cells("clmBrand").Value.ToString
                    Dim section As String = m_row.Cells("clmSection").Value.ToString
                    Dim size As String = m_row.Cells("clmQTY").Value.ToString
                    Dim store As String = m_row.Cells("clmStore").Value.ToString
                    Dim price As String = m_row.Cells("clmPrice").Value.ToString
                    'Dim coupon As String = m_row.Cells("clmCoupon").Value.ToString
                    Dim coupon = CDec(ShoppingListView.Rows(0).Cells("clmCoupon").Value).ToString("c")
    
                    Dim lvItem As New ListViewItem(New String() {item, brand, section, size, store, price, coupon})
                    PriceMatchStore.Shopping_List.Items.Add(lvItem)
                    PriceMatchStore.Shopping_List.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
                    PriceMatchStore.Shopping_List.AutoResizeColumn(2, ColumnHeaderAutoResizeStyle.HeaderSize)
                    PriceMatchStore.Shopping_List.AutoResizeColumn(5, ColumnHeaderAutoResizeStyle.HeaderSize)
                    PriceMatchStore.Shopping_List.AutoResizeColumn(6, ColumnHeaderAutoResizeStyle.HeaderSize)
    
                End If
            Next
    anIm not getting an error persay but the amount is all showing $0.00, instead of the actual coupon amount of $-1.00 or whatever. Also is there a way to eliminate the cells with values that are actually 0... Like not display them or not carry them over to the list view. Not eliminate the entire row, just the cell.

  8. #8
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Listview Columkn Format

    CDec("£-0.23").ToString("c")
    CDec("-£0.23").ToString("c")
    CDec("-0.23").ToString("c")

    All of these produce valid currency formatted values so I'm afraid I'm at a loss on that one.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  9. #9
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Listview Columkn Format

    Quote Originally Posted by compgeek1979 View Post
    the amount is all showing $0.00, instead of the actual coupon amount of $-1.00 or whatever.
    Code:
     Dim coupon = CDec(ShoppingListView.Rows(0).Cells("clmCoupon").Value).ToString("c")
    Same Row, same value.


    Quote Originally Posted by compgeek1979 View Post
    Also is there a way to eliminate the cells with values that are actually 0... Like not display them or not carry them over to the list view. Not eliminate the entire row, just the cell.
    Something like:
    Code:
    Dim coupon As String
    Dim couponValue As Decimal = CDec(_row.Cells("clmCoupon").Value)
    If couponValue = 0 Then
        coupon = ""
    Else
        coupon = couponValue.ToString("c")
    End If
    
    Dim lvItem As New ListViewItem(New String() {item, brand, section, size, store, price, coupon})

  10. #10
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Listview Columkn Format

    Same Row, same value.
    Somebody's got their laser sights on. Not bad for 1.00am!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2012
    Posts
    540

    Re: Listview Columkn Format

    Awsome. That resolved it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width