gridview - Formatting currencies in a column

crvasquez

Active member
Joined
Mar 3, 2008
Messages
40
Location
Florida
Programming Experience
10+
Title should read --> Formatting mulitple currencies in a column.

Has anyone displayed currency in a column of a gridview? I have a requirement where I need to format currencies in a gridview. I can format 1 currency. The issue is if row one is US$, row two is Euro, and row 3 is Yen how do you format the currency?

Thx.
 
You can use the RowDataBound event to do custom formatting. Work with e.Row...

Getting a currency string for some culture from a number can be done with CultureInfo and ToString method:
VB.NET:
Dim amount As Single = 123
Dim c As New Globalization.CultureInfo("ja-JP")
Dim s As String = amount.ToString("C", c)
 
Can you give me a piece of sample code to work with e.row. I am familiar with the culture info.

Basically I want to check the value of col 3. If col 3 = "GBP" then I want to format col 4 accordingly.

Thx
 
I saw the code example above. I viewed the link but still can't get it to work.

Here is my example code
VB.NET:
if e.Row.RowType = DataControlRowType.DataRow then
        
            if e.Row.Cells[5].Text = "GBP" then
            
                dim c as New CultureInfo("en-gb")
                e.Row.Cells[6].Text = e.Row.Cells[6].Text.ToString(c)
                e.Row.Cells[7].Text = e.Row.Cells[7].Text.ToString(c)
                e.Row.Cells[8].Text = e.Row.Cells[8].Text.ToString(c)
            
            elseif e.Row.Cells[5].Text = "CAD"
            
                dim c as New CultureInfo("en-ca")
                e.Row.Cells[6].Text = e.Row.Cells[6].Text.ToString(c)
                e.Row.Cells[7].Text = e.Row.Cells[7].Text.ToString(c)
                e.Row.Cells[8].Text = e.Row.Cells[8].Text.ToString(c)
            
            elseif e.Row.Cells[5].Text == "CNY"
            
                dim c as New CultureInfo("zh-cn")
                e.Row.Cells[6].Text = e.Row.Cells[6].Text.ToString(c)
                e.Row.Cells[7].Text = e.Row.Cells[7].Text.ToString(c)
                e.Row.Cells[8].Text = e.Row.Cells[8].Text.ToString(c)
            end if
        end if
 
Last edited:
VB.NET:
Dim amount As Single = CSng(e.Row.Cells(1).Text)
Now you got a numeric data type that can be formatted as currency ("C") by a specific culture (CultureInfo).

Notice that VB.Net arrays/collections use () and not [] to access the items.
 
Back
Top