格式化datagridview单元格 [英] format datagridview cell

查看:72
本文介绍了格式化datagridview单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我试图以Windows形式格式化datagridview内的单元格值,以使其具有千位分隔符
但是问题在于,它会将.00放在数字的末尾

它将123456789转换为123,456,789.00

请问该如何解决?
谢谢

In the below code, I am trying to format the value of the cell inside a datagridview in windows form to have thousand separator
But the problem is that it places .00 at the end of the numbers
i.e.
it converts 123456789 to 123,456,789.00

How can I solve this please?
Thanks

private void dgv_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex > 0)
            {
                if (dgv.Columns[e.ColumnIndex].Name.ToLower().Contains("summary"))
                {
                    if (e.Value.ToString() != "")
                    {
                        double d = double.Parse(e.Value.ToString());
                        e.Value = d.ToString("n");
                    }
                }
            }
        }

推荐答案

尝试e.Value = d.ToString("N0");.
请参考格式化数字结果表(C#参考) [ ^ ]
Try e.Value = d.ToString("N0");.
Refer to Formatting Numeric Results Table (C# Reference)[^].




在格式化字符串以定义小数位时,语法如下.


Hi,

While formatting the string to define the decimal places, the syntax is as below.


yourdoublestring.ToString("N" + nDecimal)



其中nDecimal是所需的小数位数.

请参阅下面的链接以获取更多格式.

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx [ ^ ]



where nDecimal is the number of decimal places needed.

Refer the below link for more formats.

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^]


这篇关于格式化datagridview单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆