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
Solved

DataGridview Currency Formating?

Posted on 2016-11-15
22
57 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
  • 10
  • 10
  • 2
22 Comments
 
LVL 50

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
 

Author Comment

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

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 50

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
 

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
 

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 50

Expert Comment

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

Author Comment

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

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
 

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 50

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
 

Author Comment

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

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 50

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
 

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 50

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
 

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 50

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
 

Author Closing Comment

by:Jess31
ID: 41898485
This worked perfectly
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

789 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