• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 101
  • Last Modified:

DataGridview Currency Formating?

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
Jess31
Asked:
Jess31
  • 10
  • 10
  • 2
1 Solution
 
Ryan ChongCommented:
it works for me... can you make sure your column is bounded to a currency/money column?
0
 
Jess31Author Commented:
I believe the column in the Datatable is Single.
0
 
Ryan ChongCommented:
currently how's the output of column: RetailPriceOriginalCur in your datagridview?

and what's the expected output?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Jess31Author Commented:
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
 
Ryan ChongCommented:
what about trying Globalization.CultureInfo.CurrentCulture?

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

Open in new window

0
 
Jess31Author Commented:
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
 
Jess31Author Commented:
Btw, the culture works when I do it say in a msgbox, just not working here in DataGridView.
0
 
Ryan ChongCommented:
what .NET Framework you're using?
0
 
Jess31Author Commented:
I think its 4.6. How can I check?
0
 
Ryan ChongCommented:
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
 
Jess31Author Commented:
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
 
Ryan ChongCommented:
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
 
Jess31Author Commented:
Actually on column in GBP (or some other) and everything else in USD
0
 
Ryan ChongCommented:
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
 
Ryan ChongCommented:
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
 
Jess31Author Commented:
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
 
Ryan ChongCommented:
gives error "Object reference not set to an instance of an object."
can you post your codes here?
0
 
Jess31Author Commented:
            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
 
ArkCommented:
With DGVSelectedBooks.Columns("RetailPriceOriginalCur").DefaultCellStyle
    .Format = "c2"
    .FormatProvider = Globalization.CultureInfo.GetCultureInfo("en-GB")
' more formatting if need
End With

Open in new window

0
 
ArkCommented:
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
 
Ryan ChongCommented:
@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
 
Jess31Author Commented:
This worked perfectly
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 10
  • 10
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now