【问题标题】:c# Datacolumn in datatable set decimal place [duplicate]c#数据表中的数据列设置小数位[重复]
【发布时间】:2020-09-30 18:29:59
【问题描述】:

我在这样创建的 DataTable 中有一个 DataColumn:

        DataTable oDt = new DataTable("mytable");
        oDt .Columns.Add("price", Type.GetType("System.Decimal"));

当我输入一个值时

        DataRow oRow = oDt .NewRow();
        oRow["price"] = 1;
        oDt.Rows.Add(oRow);

我只看到 1 而不是 1.00,因为它是一个十进制列。 我哪里错了?如何自动看到两位小数? 要查看数据,我使用 DataGridView

【问题讨论】:

  • DataTable和DataRow是数据类,不是UI类,你用什么来显示数据?
  • 为了查看我使用 DataGridView 的数据,我更新了我的帖子

标签: c# datatable datacolumn


【解决方案1】:

你可以这样做:

int priceIndex = 1; // Index of the price column
dataGridView.Columns[priceIndex].DefaultCellStyle.Format = "0.00##"

如果您没有价格列的索引,那么您可以直接使用列名:

dataGridView.Columns["price"].DefaultCellStyle.Format = "0.00##"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-27
    • 1970-01-01
    • 2012-08-21
    • 2019-03-31
    • 2020-07-11
    • 2018-11-30
    • 2010-11-23
    • 1970-01-01
    相关资源
    最近更新 更多