Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

show percentage by specifying DefaultCellStyle.Format value in datagridviewcolum

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

like image 675
Ramji Avatar asked Nov 13 '09 22:11

Ramji


People also ask

How do I set the format for specific columns in datagridview?

Set the Format property of a DataGridViewCellStyle. The following code example sets the format for specific columns using the DefaultCellStyle property of the columns. Values in the UnitPrice column appear in the current culture-specific currency format, with negative values surrounded by parentheses.

How do I change the default cell style in datagridview?

Select the DataGridView control in the designer. In the Properties window, click the ellipsis button () next to the DefaultCellStyle, ColumnHeadersDefaultCellStyle, or RowHeadersDefaultCellStyle property. The CellStyle Builder dialog box appears.

How to set the alignment for a specific column in datagridview?

Set the Alignment property of a DataGridViewCellStyle to one of the DataGridViewContentAlignment enumeration values. The following code example sets the alignment for a specific column using the DefaultCellStyle property of the column.

How do I show null values in datagridview?

If you are binding the DataGridView control to a data source that is likely to contain null values, fill in the Null Value text box. This value is displayed when the cell value is equal to a null reference ( Nothing in Visual Basic) or DBNull.Value.


1 Answers

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\\%";
like image 109
dtb Avatar answered Sep 24 '22 02:09

dtb