Solved

DataGridview Currency Formating?

Posted on 2016-11-15
22
37 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 49

Expert Comment

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

Author Comment

by:Jess31
Comment Utility
I believe the column in the Datatable is Single.
0
 
LVL 49

Expert Comment

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

and what's the expected output?
0
 

Author Comment

by:Jess31
Comment Utility
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 49

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
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
Comment Utility
Btw, the culture works when I do it say in a msgbox, just not working here in DataGridView.
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
what .NET Framework you're using?
0
 

Author Comment

by:Jess31
Comment Utility
I think its 4.6. How can I check?
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
Actually on column in GBP (or some other) and everything else in USD
0
 
LVL 49

Expert Comment

by:Ryan Chong
Comment Utility
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 49

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
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 49

Expert Comment

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

Author Comment

by:Jess31
Comment Utility
            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 27

Accepted Solution

by:
Ark earned 500 total points
Comment Utility
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 27

Expert Comment

by:Ark
Comment Utility
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 49

Expert Comment

by:Ryan Chong
Comment Utility
@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
Comment Utility
This worked perfectly
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now