Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting datagridview cell to 2 decimal places

Basically I have a datagridview which displays data from a datatable. Each cell in the datatable is a decimal with roughly 10 decimal points, how can I use the datagridview to display this data to 2 decimal points?

I have tried this (what I found in other questions):

        for (int i = 0; i < this.dgvDynamics1.Columns.Count; i++)
            this.dgvDynamics1.Columns[i].DefaultCellStyle.Format = "N2";

but it doesn't work

If you need any more info let me know

Thanks in advance

like image 421
bolt19 Avatar asked Aug 13 '14 08:08

bolt19


2 Answers

Try this:

this.dgvDynamics1.Columns.Items[i].DefaultCellStyle.Format = "0.00##";
this.dgvDynamics1.Columns.Items[i].ValueType = GetType(Double)
like image 98
reach4thelasers Avatar answered Nov 04 '22 14:11

reach4thelasers


try this

private void dgvList_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
    {
        if (e.ColumnIndex == 6 || e.ColumnIndex == 8)
        {
            e.CellStyle.Format = "N2";
        }
    }
like image 37
Michael azzar Avatar answered Nov 04 '22 15:11

Michael azzar