79903

display two different currencies in two columns into a datagridview

Question:

I am working on an existing piece of software and I have an object with some data members. Two of them are currencies (double). I need to display them into a datagridview. I know that the right solution will be create a currency object and overload the ToString() method. However I cannot change (or at least it will take me a lot of re-testing) the existing object.

I have change the format value in the DefaultCellStyle and the result is almost what I want.

myCol.DefaultCellStyle.Format = "C2"; display $23.56

I would like to customize the currency symbol as well.

Any ideas?

<strong>EDIT</strong>

I think I have found the solution:

myCol.DefaultCellStyle.FormatProvider = CultureInfo.GetCultureInfo("de-DE"); //display the euro symbol

Answer1:

I think you should format the value by code using a different NumberFormatInfo for the additional column, in there you can specify a different currency symbol.

Answer2:

You solution will output something like

<blockquote>

123,123.00€

</blockquote>

If you want it looks like

<blockquote>

€123,123.00

</blockquote>

you can work on the CellFormatting event, and put the currency symbol in front.

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if (e.ColumnIndex == 0 && e.RowIndex != this.dataGridView1.NewRowIndex) //set your columnindex accordingly.. { double d = double.Parse(e.Value.ToString()); e.Value = string.Format(CultureInfo.CreateSpecificCulture("de-DE"), "{0:C}", d); } }

BTW: Decimal is a better choice for money type object.

Recommend

  • changing row backcolor in datagridview
  • Updating multiple rows on a large handsontable locks chrome
  • Sort pandas dataframe both on values of a column and index?
  • Find all table names with column name? [duplicate]
  • Multithreading with Datagridview C#
  • Update and append unless empty
  • Multicolumn dropDownList - use different colors for each column
  • How do I bind a DataGridViewComboBoxColumn to a property/method of an object that returns a list?
  • Windows UTF-8 printed with chcp 65001 - characters are mysteriously duplicated
  • What is the difference in Angular2 between inject a provider in @Component and @Module?
  • Check if one value in one column is in another column
  • Range Multiplication VB.NET (What is wrong with this code?)
  • Tips for creating scalable WPF user control
  • JOOQ nested condition
  • Regex for Specific Tag
  • How can we prepend rows to a react native list-view?
  • joining two bezier curves
  • Why use database factory in asp.net mvc?
  • Implementation of State Monad
  • What's the purpose of QString?
  • Textfile Structure (tables)
  • Jackson Parser: ignore deserializing for type mismatch
  • preg_replace Double Spaces to tab (\\t) at the beginning of a line
  • ImageMagick, replace semi-transparent white with opaque white
  • Email format validation in mvc3 view
  • Cannot connect to cassandra from Spark
  • Fill an image in a square container while keeping aspect ratio
  • sending/ receiving email in Java
  • Font Awesome Showing Box instead of Icons
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Calling of Constructors in a Java
  • php design question - will a Helper help here?
  • Traverse Array and Display in markup
  • Transpose CSV data with awk (pivot transformation)
  • AngularJs get employee from factory
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Is it possible to post an object from jquery to bottle.py?
  • Python/Django TangoWithDjango Models and Databases