Formatting datagridview cell to 2 decimal places

爱⌒轻易说出口 提交于 2020-08-19 17:01:18

问题


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


回答1:


Try this:

this.dgvDynamics1.Columns.Items[i].DefaultCellStyle.Format = "0.00##";
this.dgvDynamics1.Columns.Items[i].ValueType = GetType(Double)



回答2:


try this

private void dgvList_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
    {
        if (e.ColumnIndex == 6 || e.ColumnIndex == 8)
        {
            e.CellStyle.Format = "N2";
        }
    }



回答3:


This is an old post, but for the record, this should work:

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


来源:https://stackoverflow.com/questions/25281427/formatting-datagridview-cell-to-2-decimal-places

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!