Search code examples
c#winformsdatagridviewcurrencycurrency-formatting

Use a custom currency symbol for a DataGridView column?


I want to format a DataGridView column which shows money values, however I want to use a custom currency prefix, which is not currently defined by any culture. I want my values to look like this:

Mn. 2,300.00
Mn. 40,259.22
Mn. 6.33
Mn. 2,000,203.19

Obviously, Mn. is the prefix I want to add. Other than this customization, the value itself is to be formatted like any other currency (i.e. 2 decimal places, commas after every 3 digits, etc ..)


Solution

  • You'll want to manage your own NumberFormatInfo object, which contains a CurrencySymbol property. You can pass this object to the decimal.ToString() method.

    var format = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
    format.CurrencySymbol = "Mn. ";
    var amount = 12345.67m;
    Console.WriteLine(amount.ToString("C", format));