Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

.net - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...