DataGridView十进制值格式:kosher方式 [英] DataGridView decimal value formatting: kosher way

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

问题描述

我需要在DataGridView中显示十进制值

I need to display decimal values in DataGridView

        if (dgvClients.SelectedRows.Count != 0)
        {
            DataGridViewRow row = dgvClients.SelectedRows[0];

            row.Cells["NetAccountValue"].Value = info.DecimalValue;
        }

我需要将值设置为小数分隔符后的给定数字。

I need to format value up to given digits after decimal separator.

问题是单元格 Value 属性在我的情况下存储对十进制的引用。当显示decimal.ToString()被调用,并且默认decimal.ToString()生成未格式化的字符串
,带有很多数字。

The problem is that cell Value property stores reference to decimal in my case. When displayed decimal.ToString() is called and default decimal.ToString() produces unformatted string with lots of digits.

一种方法是创建字符串和使用String.FormatString()来实现所需的格式
,并将其输入值而不是十进制。

One way is to create string and use String.FormatString() to achieve desired formatting and feed it Value instead of decimal.

还有其他方法吗?

推荐答案

使用代码可以设置 DataGridView.DefaultCellStyle属性

您还可以在设计器中执行以下操作: p>

You can also do this in the designer by:


  1. 右键单击您的DGV和
    选择编辑列。编辑
    列对话框出现

  2. 在对话框的左侧
    上选择您的列,然后单击
    DefaultCellStyle属性字段为
    打开CellStyle Builder。

  3. 在Builder对话框中,单击格式
    ,其中显示格式字符串
    对话框。

  4. 在此对话框中选择
    (选择数字类型,允许指定小数位数显示),并保存
    您的更改。

  1. Right clicking your DGV and select Edit Columns. The Edit Columns dialog appears.
  2. Select your column on the left side of the dialog and click the DefaultCellStyle property field to bring up the CellStyle Builder.
  3. In the Builder dialog click Format which brings up the Format String Dialog.
  4. Make your selections in this dialog (select Numeric type, which allows specifying number of decimals to display) and save your changes.

这篇关于DataGridView十进制值格式:kosher方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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