Kage
Kage

Reputation: 486

c# display number in datagrid with decimal separator

i bet im not looking well but I cannot seem to find the answer to the following question:

i have a Datagrid which will display multiple columns with numbers, some will be above the thousand however i cannot seem to get it to display a decimal separator, I'm saving the numbers as a decimal value but it wont display it.

Thanks for taking the time to look at my question :)

Edit: this is about showing it in a datagrid in Windows Forms

Upvotes: 0

Views: 4836

Answers (2)

gyousefi
gyousefi

Reputation: 351

For DataGridView, whose DataGridViewTextBoxColumn does not have the Format property, we need to set the format as follow:

var col = new DataGridViewTextBoxColumn();
var colFormat = new DataGridViewCellStyle();
colFormat.Format = "N0";
col.DefaultCellStyle = colFormat;

Upvotes: 0

Douglas
Douglas

Reputation: 54877

Try setting the Format property on your DataGridTextBoxColumn. The format you’re after is presumably "N0", which shows the thousands separator but no decimal places.

 DataGridTextBoxColumn textColumn = new DataGridTextBoxColumn();
 textColumn.HeaderText = "My Numbers";
 textColumn.ReadOnly = true;
 textColumn.Format = "N0";
 text

Edit: If you want your numbers to always format consistently, irrespective of the culture of the machine on which the application is run, then you should set the FormatInfo as well.

The CultureInfo for the computer is used to determine the actual currency format. You can change the CultureInfo for a DataGridTextBoxColumn by setting the FormatInfo property to a new CultureInfo instance constructed with an appropriate culture ID.

For example, if you’re in Germany and always want the thousands separator to appear as a period (e.g. 12.345.678) rather than a comma (e.g. 12,345,678), use:

 textColumn.FormatInfo = new CultureInfo("de-DE");

You might achieve better consistency by changing the culture for the entire application by setting the Application.CurrentCulture property on startup.

Upvotes: 3

Related Questions