开发者

show percentage by specifying DefaultCellStyle.Format value in datagridviewcolum

开发者 https://www.devze.com 2022-12-11 23:14 出处:网络
With datagridview.Columns(\"PricePerUnit\") .ValueType = Type.GetType(\"System.Decimal\") .DefaultCellStyle.Format = \"C\"
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

Similarly I want to show up something like If I just add row with a value five to the column "DiscountPercentage"

it should show as 5.00%

I need a string value to assign to DefaultCellSt开发者_Python百科yle.Format to achieve this.

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%

Any ideas?


Resolved

dtb Helped me do this (thanks to him)

number.ToString("0.00\%") gets the decimal number along with 2 decimal integers


It seems you need a Custom Numeric Format String here.

In C#:

12m.ToString("0\\%");  // returns "12%"

So this should do the trick:

pricePerUnitColumn.DefaultCellStyle.Format = "0\\%";


Why not just add the value .05 instead of 5? Or is that not an option?

0

精彩评论

暂无评论...
验证码 换一张
取 消