English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Formatting GridView with DataFormatString
When displaying data in a GridView, if you want to display data with many decimal places but only show two decimal places, in Delphi, you can directly use DisplayFormat. In .NET, after searching through MSDN for a long time, I found that DataFormatString can achieve this function, but it didn't work. Finally, I found out that2.0 For safety reasons, you also need to set HtmlEncode = false to make DataFormatString effective.
Leave a mark so that you don't have to waste a lot of time next time you use it.
Moreover, DataFormatString = "{0:F}" is the default format, showing two decimal places. If you need to display a different number of decimal places, you can use DataFormatString = "{0:Fn}".
DataFormatString="{0:format string}"
In the DataFormatString, {0} represents the data itself, while the format string after the colon represents the format in which we want the data to be displayed;
Number, currency format:}}
After the specified format symbol, you can specify the number of decimal places to be displayed. For example, the original data is「1.56」,if the format is set to {0:N1} then the output is「1.5」。ItsCommon number formats are as follows:
Format string Input Result
"{0:C}" 12345.6789 $12,345.68
"{0:C}" -12345.6789 ($12,345.68)
"{0:D}" 12345 12345
"{0:D8") 12345 00012345
"{0:E}" 12345.6789 1234568E+004
"{0:E10") 12345.6789 1.2345678900E+004
"{0:F}" 12345.6789 12345.68
"{0:F0}" 12345.6789 12346
"{0:G}" 12345.6789 12345.6789
"{0:G7") 123456789 1.234568E8
"{0:N}" 12345.6789 12,345.68
"{0:N4") 123456789 123,456,789.0000
"Total: {0:C}" 12345.6789 Total: $12345.68
Common date-time formats:
Format Description Output format
d Compact date format MM/dd/yyyy
D Detailed date format dddd, MMMM dd, yyyy
f Complete format (long date + short time) dddd, MMMM dd, yyyy HH:mm
F
Complete date-time format
(long date + long time)
dddd, MMMM dd, yyyy HH:mm:ss
g General format (short date + short time) MM/dd/yyyy HH:mm
G General format (short date + long time) MM/dd/yyyy HH:mm:ss
m,M Month day format MMMM dd
s Moderate date-time format yyyy-MM-dd HH:mm:ss
t Compact time format HH:mm
T Detailed time format HH:mm:ss
C
Currency
2.5.ToString("C")
¥2.50
D
Decimal number
25.ToString("D5")
00025
E
Scientific
25000.ToString("E")
2.500000E+005
F
Fixed point
25.ToString("F2")
25.00
G
General
2.5.ToString("G")
2.5
N
Number
2500000.ToString("N")
2,500,000.00
X
Hexadecimal
255.ToString("X")
FF
The formatCode is an optional formatting code string. (For detailed information, please search for "Formatting String")
The format must be separated from other characters using "{" and "}". If brackets are needed within the format, use two consecutive brackets to represent one, that is: "{{" or "}}".
Common Format Examples:
(1); int i=12345;
this.textBox1.Text=i.ToString();
//Result 12345(this refers to the current object, or the instance of the current class)
this.textBox2.Text=i.ToString("d8);
//Result 00012345
(2); int i=123;
double j=123.45;
string s1=string.Format("the value is {0,7:d}",i);
string s2=string.Format("the value is {0,7:f3};
this.textBox1.Text=s1 ;
//Result the value is 123
this.textBox2.Text=s2;
//Result the value is 123.450
(3); double i=12345.6789;
this.textBox1.Text=i.ToString("f2); //Result 12345.68
this.textBox2.Text=i.ToString("f6);
//Result 12345.678900
(4); double i=12345.6789;
this.textBox1.Text=i.ToString("n"); //Result 12,345.68
this.textBox2.Text=i.ToString("n4); //Result 12,345.6789
(5); double i=0.126;
string s=string.Format("the value is {0:p}",i);
this.textBox1.Text=i.ToString("p"); //Result 12.6%
this.textBox2.Text=s; //Result the value is 12.6%
(6); DateTime dt =new DateTime(2003,5,25);
this.textBox1.Text=dt.ToString("yy.M.d");
//Result 03.5.25
this.textBox2.Text=dt.ToString("yyyy年M月");
//Result 2003Year5Month
Convert.ToDateTime("2005/12/22 22:22:22").ToString("yyyy/MM/dd HH:mm:ss")
"2005/12/22 22:22:22"
(7); int i=123;
double j=123.45;
string s=string.Format("i:{0,-7},{1,7};i,j);
//-7represents left alignment, occupying7Bit
this.textBox1.Text=s ;
//Result i:123 ,j: 123.45