Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

DataGridview Currency Formating?

Posted on 2016-11-15
22
Medium Priority
?
90 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 53

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 53

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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 53

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 53

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 53

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 53

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 53

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 53

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 53

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 2000 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 53

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

610 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