通过在datagridviewcolum中指定DefaultCellStyle.Format值来显示百分比 [英] show percentage by specifying DefaultCellStyle.Format value in datagridviewcolum
问题描述
With datagridview.Columns("PricePerUnit")
.ValueType = Type.GetType("System.Decimal")
.DefaultCellStyle.Format = "C"
End With
数据表绑定到了datagridview,而上面的
编码a如果我只向 PricePerUnit列
中添加值为5的行,则它将在datagridview列中显示为$ 5.00
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
类似地,我想显示类似如果我仅将值五的行添加到 DiscountPercentage列中
Similarly I want to show up something like If I just add row with a value five to the column "DiscountPercentage"
它应该显示为5.00%
it should show as 5.00%
我需要一个字符串值来分配给DefaultCellStyle.Format来实现。
I need a string value to assign to DefaultCellStyle.Format to achieve this.
如果我使用 DefaultCellStyle.Format = P
它会自动将其乘以100,因此对于输入5,
会显示为500.00%,而不是5.00%
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%
任何
已解决
dtb帮助我做到了(感谢他)
dtb Helped me do this (thanks to him)
number.ToSt ring( 0.00\%)
获取十进制数字以及2个十进制整数
number.ToString("0.00\%")
gets the decimal number along with 2 decimal integers
推荐答案
看来您需要在此处自定义数字格式字符串。
在C#中:
12m.ToString("0\\%"); // returns "12%"
因此,这应该可以解决问题:
So this should do the trick:
pricePerUnitColumn.DefaultCellStyle.Format = "0\\%";
这篇关于通过在datagridviewcolum中指定DefaultCellStyle.Format值来显示百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!