[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.
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")
Re: Listview Columkn Format
So what should I have used?
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.
Re: Listview Columkn Format
ok so I am getting this error. ( I actually tried this way before and it didnt work. )
Attachment 96815
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")
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.
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.
Re: Listview Columkn Format
Quote:
Originally Posted by
compgeek1979
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
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})
Re: Listview Columkn Format
Quote:
Same Row, same value.
Somebody's got their laser sights on. Not bad for 1.00am! :cool:
Re: Listview Columkn Format
Awsome. That resolved it.