Solved

DataGridview Currency Formating?

Posted on 2016-11-15
22
65 Views
Last Modified: 2016-11-22
I want to format a column as currency using CultureInfo since the currency is not static. I tried
 DGVSelectedBooks.Columns("RetailPriceOriginalCur").DefaultCellStyle.Format = String.Format("c2", Globalization.CultureInfo.CreateSpecificCulture("en-GB"))

Open in new window

But this does not seem to work.
0
Comment
Question by:Jess31
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 10
  • 2
22 Comments
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41889003
it works for me... can you make sure your column is bounded to a currency/money column?
0
 
LVL 1

Author Comment

by:Jess31
ID: 41889056
I believe the column in the Datatable is Single.
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41889066
currently how's the output of column: RetailPriceOriginalCur in your datagridview?

and what's the expected output?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Jess31
ID: 41889732
The column shows as though it were formatted in USD: $123.46. I expect to get £123.46. If I don't put any format then it shows 123.4600
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41890718
what about trying Globalization.CultureInfo.CurrentCulture?

DataGridView2.Columns("CcurDataGridViewTextBoxColumn").DefaultCellStyle.Format = String.Format("c2", Globalization.CultureInfo.CurrentCulture)

Open in new window

0
 
LVL 1

Author Comment

by:Jess31
ID: 41890723
But I don't want CurrentCulture. I am in USA. But at times I need to format as GBP or AU or other currencies.
0
 
LVL 1

Author Comment

by:Jess31
ID: 41890724
Btw, the culture works when I do it say in a msgbox, just not working here in DataGridView.
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41890732
what .NET Framework you're using?
0
 
LVL 1

Author Comment

by:Jess31
ID: 41890734
I think its 4.6. How can I check?
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41890744
this seems work for me.
System.Globalization.CultureInfo.DefaultThreadCurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB")
DGVSelectedBooks.Columns("RetailPriceOriginalCur").DefaultCellStyle.Format = String.Format("c2", Globalization.CultureInfo.CurrentCulture)

Open in new window


How can I check?
you may open the app.config and look for line such as:
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />

Open in new window

Check this out for more info:
<supportedRuntime> Element
https://msdn.microsoft.com/en-us/library/w4atty68.aspx
0
 
LVL 1

Author Comment

by:Jess31
ID: 41890791
Version=v4.5.2

when I change to defaultculture as you have it, then it changes to GBP but it does so for ALL columns, even those that I don't set and that I want to remain in USD.
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41890807
when I change to defaultculture as you have it, then it changes to GBP but it does so for ALL columns, even those that I don't set and that I want to remain in USD.
So is that mean you got one column displaying currency in USD and another column displaying currency in GBP?
0
 
LVL 1

Author Comment

by:Jess31
ID: 41890816
Actually on column in GBP (or some other) and everything else in USD
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41890823
well you probably can try this...

for the column(s) to display as GBP, try:
Dim rInfo As New System.Globalization.RegionInfo(System.Globalization.CultureInfo.CreateSpecificCulture("en-GB").LCID)
DataGridView1.Columns("YourColumn").DefaultCellStyle.Format = rInfo.CurrencySymbol & " #,#0.00"

Open in new window


for the column to display as USD, try:
Dim rInfo As New System.Globalization.RegionInfo(System.Globalization.CultureInfo.CreateSpecificCulture("en-US").LCID)
DataGridView1.Columns("YourColumn").DefaultCellStyle.Format = rInfo.CurrencySymbol & " #,#0.00"

Open in new window


if you want to use system default, then try:
DataGridView1.Columns("YourColumn").DefaultCellStyle.Format = String.Format("c2", Globalization.CultureInfo.CurrentCulture)

Open in new window

0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41890827
or something like this should work for you:

Dim rInfo As New System.Globalization.RegionInfo(System.Globalization.CultureInfo.CreateSpecificCulture("en-GB").LCID)
        System.Globalization.CultureInfo.DefaultThreadCurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-USD")
        DataGridView1.Columns("myGBPCol").DefaultCellStyle.Format = rInfo.CurrencySymbol & " #,#0.00"
        DataGridView1.Columns("myUSDCol1").DefaultCellStyle.Format = String.Format("c2", Globalization.CultureInfo.CurrentCulture)
        DataGridView1.Columns("myUSDCol2").DefaultCellStyle.Format = String.Format("c2", Globalization.CultureInfo.CurrentCulture)

Open in new window

0
 
LVL 1

Author Comment

by:Jess31
ID: 41891651
The only column that is issue is the that requires different (foreign) currency format, all others is find with the default (c2).

>DataGridView1.Columns("YourColumn").DefaultCellStyle.Format = rInfo.CurrencySymbol & " #,#0.00"
gives error "Object reference not set to an instance of an object."
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41892372
gives error "Object reference not set to an instance of an object."
can you post your codes here?
0
 
LVL 1

Author Comment

by:Jess31
ID: 41892835
            Dim rInfo As New System.Globalization.RegionInfo(System.Globalization.CultureInfo.CreateSpecificCulture("en-GB").LCID)
            DataGridView1.Columns("RetailPriceOriginalCur").DefaultCellStyle.Format = rInfo.CurrencySymbol & " #,#0.00"

Open in new window

0
 
LVL 28

Accepted Solution

by:
Ark earned 500 total points
ID: 41895192
With DGVSelectedBooks.Columns("RetailPriceOriginalCur").DefaultCellStyle
    .Format = "c2"
    .FormatProvider = Globalization.CultureInfo.GetCultureInfo("en-GB")
' more formatting if need
End With

Open in new window

0
 
LVL 28

Expert Comment

by:Ark
ID: 41895204
Sample with different currency formats
Private Class prices
    Public Property Price1 As Single
    Public Property Price2 As Single
    Public Property Price3 As Single
End Class
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    DataGridView1.AutoGenerateColumns = True
    Dim priceList As New List(Of prices)
    priceList.Add(New prices With {.Price1 = 12.25, .Price2 = 1234.12, .Price3 = 1234567})
    DataGridView1.DataSource = priceList
    With DataGridView1.Columns(0).DefaultCellStyle
        .Format = "c2"
        .FormatProvider = Globalization.CultureInfo.GetCultureInfo("en-US")
        .ForeColor = Color.Green
    End With
    With DataGridView1.Columns(1).DefaultCellStyle
        .Format = "c2"
        .FormatProvider = Globalization.CultureInfo.GetCultureInfo("en-GB")
        .ForeColor = Color.Blue
    End With
    With DataGridView1.Columns(2).DefaultCellStyle
        .Format = "c2"
       ' Not need - it's by default
       ' .FormatProvider = Globalization.CultureInfo.CurrentCulture
        .ForeColor = Color.Red
    End With
End Sub

Open in new window

0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41898478
@Jess31,
Based on your comment at:
https://www.experts-exchange.com/questions/28983314/DataGridview-Currency-Formating.html#a41892835

Do you have a datagrid view called as DataGridView1?
0
 
LVL 1

Author Closing Comment

by:Jess31
ID: 41898485
This worked perfectly
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Copying from a network share 3 37
Send SMS from vb.net desktop app 30 51
Sending HTML Formatted Email from ASP.NET 2 55
VB .net 2010 Byte array 2 21
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question