Monday, June 29, 2009

Display Money value of countries in their own currency in DataGridView

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.


[CODE]

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

[/CODE]

OUTPUT

[CODE]

US UK Denmark Australia Canada

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

[/CODE]


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

No comments:

Post a Comment