DataGridView十进制值格式化:犹太方式

时间:2009-12-11 17:55:08

标签: winforms datagridview formatting decimal

我需要在DataGridView

中显示十进制值
        if (dgvClients.SelectedRows.Count != 0)
        {
            DataGridViewRow row = dgvClients.SelectedRows[0];

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

我需要在小数点分隔符后将值格式化为给定的数字。

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

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

还有其他方法吗?

2 个答案:

答案 0 :(得分:1)

使用代码,您可以设置DataGridView.DefaultCellStyle property

您也可以通过以下方式在设计师中执行此操作:

  1. 右键单击您的DGV和 选择编辑列。编辑 将出现“列”对话框。
  2. 选择左侧的列 对话框并单击 DefaultCellStyle属性字段 调出CellStyle Builder。
  3. 在“构建器”对话框中,单击“格式” 它会调出Format String 对话框。
  4. 在此对话框中进行选择 (选择数字类型,允许指定要显示的小数位数)并保存 你的改变。

答案 1 :(得分:0)

我发现这个解决方案很简单。我已经搜索了两天,终于找到了。 - 您的db列类型必须是Money或number,double,single。 - 更新datagridview事件“CellFormatting”,如下面的代码。

private void dataGridView2_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
   {
       if (this.dataGridView2.Columns[e.ColumnIndex].Name == "Aidat")
       {
          string deger=(string)e.Value;
          deger = String.Format("{0:0.00}", deger);
       }
   }