0

I have an query with number format.When changed the decimal separator(.) ---> (,) by using Region settings, type the (1.5) in excel sheet it will changed as (1,5) correctly.
My problem is in DataGridView control, I followed the same procedure in DataGrid , but it displayed (1.5) ---> (15). The (,) operator has removed. I need to know, is this the actual behaviour of the DataGrid. Can we perform same like as excel? I have tried below code.

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

CultureInfo culture = CultureInfo.CurrentUICulture;
culture.NumberFormat.NumberDecimalSeparator = ",";
culture.NumberFormat.NumberGroupSeparator = ".";

Please refer comparison image of excel and DataGrid enter image description here

Please any one suggest me how to achieve this like excel???

Sujeet Sinha
  • 2,417
  • 2
  • 18
  • 27
Prithiv
  • 504
  • 5
  • 20
  • I think you want to set the properties of the `NumberFormat` for the `CurrentCulture`, not the `CurrentUICulture`. Also, you may need to pass `true` as a second parameter to the `CultureInfo`'s constructor in order for your overrides to be used. – wablab Jun 24 '16 at 19:09
  • Actually, disregard that part about passing `true` for the second parameter to the `CultureInfo` constructor. It's unnecessary. – wablab Jun 24 '16 at 19:15

2 Answers2

2

To apply a custom number format to the column, you should perform these settings:

  • To set number format for a column to show thousand separator and define number of digits after decimal point (for example 2 digits), you should assign "N2" to DefaultCellStyle.Format property of the Column.

  • To use a custom thousand separator character and a custom decimal point character, you should create a CultureInfo based on a culture for example "en-US" and then change its NumberFormat.NumberDecimalSeparator and NumberFormat.NumberGroupSeparator and set the culture as DefaultCellStyle.FormatProvider of the Column.

  • Also if the column is a bound column, set its ValyeType to a numeric type. And if it's a bound column, make sure the underlying column in database has one of above types.

Example

Below code adds an unbound column to a grid and use a format for the column with title "Column One" which it shows 1234567.89 like 1.234.567,89. It uses "." as thousand separator and "," as decimal point:

this.dataGridView1.Columns.Add(new DataGridViewTextBoxColumn()
{
    ValueType = typeof(double),
    Name = "Column1",
    HeaderText = "Column One"
});
var culture= CultureInfo.CreateSpecificCulture("en-US");
culture.NumberFormat.NumberDecimalSeparator=",";
culture.NumberFormat.NumberGroupSeparator=".";
this.dataGridView1.Columns["Column1"].DefaultCellStyle.FormatProvider = culture;
this.dataGridView1.Columns["Column1"].DefaultCellStyle.Format = "N2";
Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • @Prithiv It seems the post answers your question. Let me know if you have any question about the answer. Since you have not voted or accepted any post in your previous questions, take a look at [Accepting Answers: How does it work?](http://meta.stackexchange.com/a/5235) – Reza Aghaei Jun 27 '16 at 20:03
  • Hi Reza, i have tried this all solutions. but i can't resolve my problem. I have checked with culture(it-IT), but still that issue replicates. – Prithiv Jun 29 '16 at 17:29
  • 1
    Hi @Prithiv the solution is tested. To test it, simply create an unbound datagridview and put these codes in form `Load` event handler. Then you will have a grid with a column which uses `.` as thousand separator and `,` as decimal point. After you finished typing in a cell and going to next cell, you will see the the result. – Reza Aghaei Jun 29 '16 at 19:12
  • Let me know if you have any question about the answer or if you find it helpful :) – Reza Aghaei Sep 24 '16 at 10:48
1

I see a few problems.

  1. You need to set your decimal and group separator strings on the NumberFormat object for the CurrentCulture, not the CurrentUICulture.
  2. You need to ensure that the underlying type to which you are binding that data grid column is a floating point type, such as decimal. I suspect that currently it is an integral type, such as int.
wablab
  • 1,703
  • 13
  • 15