Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Number Format For Datagridview in C#

I want to set the datagridview values in number format. Currently I am getting this:

enter image description here

I want something like this:

enter image description here

I have used this code:

dgvmain.DefaultCellStyle.Format = "#.##0";

And this code

dgvmain.Columns["Amount"].DefaultCellStyle.Format = "#.##0";

But neither of this working.

like image 959
ROM Avatar asked Dec 26 '22 22:12

ROM


2 Answers

You can Set you Format String using CellStyle Builder an set the Custom format to # mm

How to do it :

  1. Right click on Grid, then Properties
  2. In the property window, click the button that will popup up the Edit Columns Dialog
  3. Select the cell you want to format
  4. On the right side of the Edit Columns Dialog select the DefaultCellStyle property Click the DefaultCellStyle property, then the CellStyleBuilder dialog will open Here you have the format property, this will give you the Format String Dialog
  5. Set the Custom property to N2 you will see the preview at the bottom
  6. Click OK ... till you are back to your Grid...

If you want to do it from Code, put this code inside Form_load event.

dgvmain.Columns["Amount"].DefaultCellStyle.Format = "N2";
like image 178
abhishek Avatar answered Dec 31 '22 14:12

abhishek


Goto your Form's Design mode then goto the properties of dgvmain there check DefaultCellStyle and set the format as you like.

OR

you can do this

dgvmain.Columns["Amount"].DefaultCellStyle.Format = "N2";

:)

like image 22
Muneeb Mirza Avatar answered Dec 31 '22 14:12

Muneeb Mirza