C# 通过在 datagridviewcolum 中指定 DefaultCellStyle.Format 值来显示百分比
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1732311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
show percentage by specifying DefaultCellStyle.Format value in datagridviewcolum
提问by Ramji
With datagridview.Columns("PricePerUnit")
.ValueType = Type.GetType("System.Decimal")
.DefaultCellStyle.Format = "C"
End With
A datatable is bound to the datagridview and in the above code a If I just add row with a value five to the column "PricePerUnit" it will be shown as $5.00 in the datagridview column
数据表绑定到 datagridview,在上面的代码中,如果我只是将值为 5 的行添加到“PricePerUnit”列中,它将在 datagridview 列中显示为 $5.00
Similarly I want to show up something like If I just add row with a value five to the column "DiscountPercentage"
同样,我想显示一些类似的东西,如果我只是在“DiscountPercentage”列中添加值为 5 的行
it should show as 5.00%
它应该显示为 5.00%
I need a string value to assign to DefaultCellStyle.Format to achieve this.
我需要一个字符串值来分配给 DefaultCellStyle.Format 来实现这一点。
If I use DefaultCellStyle.Format="P"
it automatically multiplies that to 100 and so
for a input of 5 it shows as 500.00% instead of 5.00%
如果我使用DefaultCellStyle.Format="P"
它自动将其乘以 100,因此对于 5 的输入,它显示为 500.00% 而不是 5.00%
Any ideas?
有任何想法吗?
Resolved
解决
dtb Helped me do this (thanks to him)
dtb 帮我做到了(感谢他)
number.ToString("0.00\%")
gets the decimal number along with 2 decimal integers
number.ToString("0.00\%")
获取十进制数和 2 个十进制整数
采纳答案by dtb
It seems you need a Custom Numeric Format Stringhere.
看来您在这里需要一个自定义数字格式字符串。
In C#:
在 C# 中:
12m.ToString("0\%"); // returns "12%"
So this should do the trick:
所以这应该可以解决问题:
pricePerUnitColumn.DefaultCellStyle.Format = "0\%";
回答by zincorp
Why not just add the value .05 instead of 5? Or is that not an option?
为什么不添加值 0.05 而不是 5?或者这不是一个选择?