Resources » .NET programming » .NET Framework

Display Money value of countries in their own currency in DataGridView


Posted Date: 29-Jun-2009  Last Updated:   Category: .NET Framework    
Author: Member Level: Gold    Points: 15



In this article, we will discuss how to display Money value of countries in their own currency in DataGridView cells.

The System.Globalization.CultureInfo Class is used to find the currency symbol for various countries (locale).

To illustrate this, we can display money values for various countries in a datagridview.

Prerequisties:

1. Drag and Drop DataGridView on Windows Form and name it as dbGridView

2. Add 5 columns to dbGridView with different country Names

1. US
2. UK
3. Denmark
4. Finland
5. Canada

3. Populate dataGridView with some values

The handling of CellFormatting event of DataGridview does the actual trick.

The System.Globalization.CultureInfo class gives the information of all countries for number formatting.

In this example I have applied currency format for US, UK, Denmark, Finland and Canada.

But there are many countries formats are available in System.Globalization.CultureInfo Class.

The list of countries supported is specified in the bottom of the article.




Private Sub PopulateGridView ()

Dim dt As DataTable = New DataTable()

Dim US As DataColumn
Dim UK As DataColumn
Dim Denmark As DataColumn
Dim Finland As DataColumn
Dim Canada As DataColumn

'Data Columns for various countries

US = New DataColumn("US", System.Type.GetType("System.Decimal"))
UK = New DataColumn("UK", System.Type.GetType("System.Decimal"))

Denmark = New DataColumn("Denmark", System.Type.GetType("System.Decimal"))
Finland = New DataColumn("Finland", System.Type.GetType("System.Decimal"))
Canada = New DataColumn("Canada", System.Type.GetType("System.Decimal"))


dt.Columns.Add(US)
dt.Columns.Add(UK)
dt.Columns.Add(Denmark)
dt.Columns.Add(Finland)
dt.Columns.Add(Canada)

dt.AcceptChanges()

'Bind the data table to DataGridview

dbGridView.dataSource = dt

End Sub


'Format each cell with respective country's currency sign

Private Sub dbGridView_CellFormatting(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) _
Handles DataGridView1.CellFormatting


Dim MyCellValue As Decimal

Dim MyUSCultureInfo As System.Globalization.CultureInfo

'United States
If e.ColumnIndex = 0 Then
MyUSCultureInfo = New System.Globalization.CultureInfo("en-US")

If Not e.Value Is Nothing Then

MyCellValue = Convert.ToDecimal(e.Value)
e.Value = MyCellValue.ToString("c", MyUSCultureInfo)
End If

'Great Britain
ElseIf e.ColumnIndex = 1 Then
MyUSCultureInfo = New System.Globalization.CultureInfo("en-GB")

If Not e.Value Is Nothing Then

MyCellValue = Convert.ToDecimal(e.Value)
e.Value = MyCellValue.ToString("c", MyUSCultureInfo)
End If

'Denmark
ElseIf e.ColumnIndex = 2 Then
MyUSCultureInfo = New System.Globalization.CultureInfo("da-DK")

If Not e.Value Is Nothing Then

MyCellValue = Convert.ToDecimal(e.Value)
e.Value = MyCellValue.ToString("c", MyUSCultureInfo)
End If

'Finland
ElseIf e.ColumnIndex = 3 Then
MyUSCultureInfo = New System.Globalization.CultureInfo("fi-FI")

If Not e.Value Is Nothing Then

MyCellValue = Convert.ToDecimal(e.Value)
e.Value = MyCellValue.ToString("c", MyUSCultureInfo)
End If

'Canada
ElseIf e.ColumnIndex = 4 Then
MyUSCultureInfo = New System.Globalization.CultureInfo("en-CA")

If Not e.Value Is Nothing Then

MyCellValue = Convert.ToDecimal(e.Value)
e.Value = MyCellValue.ToString("c", MyUSCultureInfo)
End If

End If

End Sub



OUTPUT



US UK Denmark Australia Canada

$0.00 £0.00 kr. 0,00 0,00 € $0.00




Some of other cultures are:

ar-DZ Arabic - Algeria
ar-IQ Arabic - Iraq
bg-BG Bulgarian - Bulgaria

en-IE English - Ireland

en-NZ English - New Zealand

en-PH English - Philippines

en-ZA English - South Africa

en-TT English - Trinidad and Tobago

en-GB English - United Kingdom

en-US English - United States

en-ZW English - Zimbabwe

et Estonian

et-EE Estonian - Estonia

fo Faroese

fo-FO Faroese - Faroe Islands

fa Farsi

fa-IR Farsi - Iran

fi Finnish

fi-FI Finnish - Finland

fr French


Reference: http://vijirajkumar.blogspot.com/2009/06/display-money-value-of-countries-in.html


Did you like this resource? Share it with your friends and show your love!

Responses to "Display Money value of countries in their own currency in DataGridView"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    ujjwala sinha
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India