C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Display Money value of countries in their own currency in DataGridView


Posted Date: 29 Jun 2009    Resource Type: Articles    Category: .NET Framework
Author: Viji RAJKUMARMember Level: Diamond    
Rating: 1 out of 5Points: 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






For more details, visit http://vijirajkumar.blogspot.com/2009/06/display-money-value-of-countries-in.html



Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Display Money value of countries in their own currency in DataGridView  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: DataTypes in C#
Previous Resource: Drag and Drop made easy
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use