|
-
Feb 22nd, 2013, 12:33 AM
#1
Thread Starter
Fanatic Member
[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.
-
Feb 22nd, 2013, 01:06 AM
#2
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")
-
Feb 22nd, 2013, 01:20 AM
#3
Thread Starter
Fanatic Member
Re: Listview Columkn Format
So what should I have used?
-
Feb 22nd, 2013, 01:49 AM
#4
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.
-
Feb 22nd, 2013, 12:12 PM
#5
Thread Starter
Fanatic Member
Re: Listview Columkn Format
ok so I am getting this error. ( I actually tried this way before and it didnt work. )
Attachment 96815
-
Feb 22nd, 2013, 05:14 PM
#6
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!
-
Feb 22nd, 2013, 06:59 PM
#7
Thread Starter
Fanatic Member
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.
-
Feb 22nd, 2013, 07:44 PM
#8
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!
-
Feb 22nd, 2013, 07:54 PM
#9
Re: Listview Columkn Format
 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.
 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})
-
Feb 22nd, 2013, 08:09 PM
#10
Re: Listview Columkn Format
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!
-
Feb 23rd, 2013, 01:10 AM
#11
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|