Akrem
Akrem

Reputation: 4652

formatting decimal value in column in datagridview

I want to formatting a column of sum value: for example I have 19240 I want it 192.40 I tried many way but nothing

dgw.Grid.Columns["Sum"].DefaultCellStyle.Format="0:00" // 192:40

dgw.Grid.Columns["Sum"].DefaultCellStyle.Format="n"    // 19240.00

dgw.Grid.Columns["Sum"].DefaultCellStyle.Format="0.00" // 19240.00

dgw.Grid.Columns["Sum"].DefaultCellStyle.Format=string.Format("{0:0.##}") // Exception

I want all items in that column in datagrid view with the same format

edit : a way to do it:

I use this methode to get what I want

    int _sum;
    public int Sum
    {
        get { return _sum; }
        set { _sum= value; }
    }

    public double SumAsdouble
    {
        get
        {
            if (_sum== 0)
                return 0;
            else
                return Convert.ToDouble(_sum) / 100;

        }
    }

and I make hidden the Sum and show the SumAsdouble

and this work for me

Upvotes: 0

Views: 12128

Answers (2)

PVitt
PVitt

Reputation: 11740

I assume that you always want to have the last two digits to be decimal digits and that your number is always five digits long. Then you can use "000.00" or "%" as format string. Alternatively, you can devide your number by 100.

Upvotes: 0

Paddy
Paddy

Reputation: 33857

What is the value in the cell?

It looks like it is 19240, which means that formatting it to 2DP will return 19240.00, as you are seeing. If you want 192.40, then you'll need to divide by 100 first - you'll not be able to do this with string formats.

Upvotes: 1

Related Questions