This is a migrated thread and some comments may be shown as answers.

RadGridView and currency FormatInfo

2 Answers 410 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Rafael
Top achievements
Rank 1
Rafael asked on 30 Aug 2010, 12:36 AM
Hello,

I'm using a RadGridView to view a List<Products>. It contains a decimal "Price" field and a string "currency" field.
I've figured out how to specify a column FormatInfo and FormatString:

CultureInfo cultureInfo;
 switch (Product.Currency)
 {
     case "EUR":
         cultureInfo = new CultureInfo("fr-FR");
         break;
     case "GBP":
         cultureInfo = new CultureInfo("en-GB");
         break;
     default:
         cultureInfo = new CultureInfo("fr-FR");
         break;
 }
 radGridViewSupplierProducts.MasterGridViewTemplate.Columns["Price"].FormatInfo = cultureInfo;

Now the question is: how can i do the same thing but cell by cell (not for the entire column), based on the Product fields values.
ex: one product that costs £23 and another 45€ and displaying those values..

Regards,

Rafael

2 Answers, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 01 Sep 2010, 04:41 PM
Hi Rafael,

We do not support setting different cultures to cells of a single column. But you can still achieve your scenario by subscribing to the CellFormatting event where you can use the Text property of the CellElement:
private void radGridView1_CellFormatting(object sender, CellFormattingEventArgs e)
{
    GridViewColumn col = e.CellElement.ColumnInfo as GridViewColumn;
 
    if (col.Name != "ID")
    {
        return;
    }
 
    CultureInfo culture = null;
 
    if (e.CellElement.RowIndex % 2 == 0)
    {
        culture = new CultureInfo("fr-FR");
    }
    else
    {
        culture = new CultureInfo("en-GB");
    }
 
    e.CellElement.Text = String.Format(culture, "{0:c}", e.CellElement.Value);
 
}

I hope this helps.

Greetings,
Svett
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Rafael
Top achievements
Rank 1
answered on 02 Sep 2010, 08:41 AM
Hi!

Thanks for your help, i had a little difficulty in changed formatting the price, basing on th ecurrency (another cell) but it ended up working like this:

private void radGridViewSuppliers_CellFormatting(object sender, CellFormattingEventArgs e)
{
 
    try
    {
        GridViewColumn col = e.CellElement.ColumnInfo as GridViewColumn;
        if (col.Name != "PurchasePriceHT")
            return;
 
        CultureInfo culture = null;
        string currency = ((SupplierWinePrice)(e.CellElement.RowInfo.DataBoundItem)).Supplier.Country.Currency;
        switch (currency)
        {
            case "EUR":
                culture = new CultureInfo("fr-FR");
                break;
            case "GBP":
                culture = new CultureInfo("en-GB");
                break;
            default:
                culture = new CultureInfo("fr-FR");
                break;
        }
        e.CellElement.Text = String.Format(culture, "{0:c}", e.CellElement.Value);
    }
    catch (Exception ex)
    {
        LogManager.LogError("ProductsManager", ex);
    }
}

Regards,

Rafael
Tags
GridView
Asked by
Rafael
Top achievements
Rank 1
Answers by
Svett
Telerik team
Rafael
Top achievements
Rank 1
Share this question
or