c# - 在 System.Windows.Forms.DataGrid 中设置 double/float/decimal 的精度

标签 c# winforms datagrid precision

如何设置 System.Windows.Forms.DataGrid 中保存 double 、 float 或十进制数的数据列的精度?

对于 DataGridView 有 How to format a column with number decimal with max and min in DataGridView? ,例如。

例如,我希望将 0.0100000001 显示为 0.01,即小数点后 2 位的精度。我想避免它们看起来像这样,其中 float 和 double 使用科学记数法:

enter image description here

我用来填充表格的代码是:

var table = new DataTable();
table.Columns.Add("double");
table.Columns.Add("float");
table.Columns.Add("decimal");
table.Columns[0].DataType = typeof(double);
table.Columns[1].DataType = typeof(float);
table.Columns[2].DataType = typeof(decimal);
table.Rows.Add(new object[] { 0.00000000000423, 0.00000000000423, 0.00000000000423 });
dataGrid1.DataSource = table;

注意:我知道 DataGrid 已过时,但我正在处理遗留代码,请不要发表评论告诉我使用 DataGridView - 这对我没有帮助。

最佳答案

我从@stuartd 评论中得出了我的解决方案。我需要设置 Formatof the current table style对于数据网格。

/// <summary>
///  Getting and setting the column widths of a DataGrid is not easy at all.
///  This helper class handles it, including saving and restoring from a string.
/// </summary>
static class DataGridColumnWidthExtensions
{
    /// Get the current table style.
    public static DataGridTableStyle GetCurrentTableStyle(this DataGrid grid)
    {
        // DataGrid holds the current grid table style into a private field called myGridTable.
        // The field points to the "default" table style even if TableStyles is empty. The 
        // default table style is also private/internal.
        // See https://stackoverflow.com/a/39832554/492336 and 
        // https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/DataGrid.cs,211.
        FieldInfo[] fields = grid.GetType().GetFields(
                     BindingFlags.NonPublic |
                     BindingFlags.Instance);

        return (DataGridTableStyle)fields.First(item => item.Name == "myGridTable").GetValue(grid);
    }
}

然后我们可以迭代 GridColumnStyles 并为每个数字列设置 Format 属性:

var tableStyle = dataGrid1.GetCurrentTableStyle();
for (int ii = 0; ii < table.Columns.Count; ii++)
{
    var columnStyle = tableStyle.GridColumnStyles[ii] as DataGridTextBoxColumn;
    if (columnStyle == null)
    {
        // DataGridTextBoxColumn inherits DataGridColumnStyle but in theory
        // a column might be of some other type deriving from DataGridColumnStyle.
        continue;
    }

    var columnType = table.Columns[ii].DataType;
    if (columnType != typeof(double) && columnType != typeof(float) && columnType != typeof(decimal))
    {
        // We set the format only for numeric columns.
        continue;
    }

    // 2 digits after the decimal mark.
    columnStyle.Format = "N2";
}

关于c# - 在 System.Windows.Forms.DataGrid 中设置 double/float/decimal 的精度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39847445/

相关文章:

c# - 如何在运行时更改数据模板?

c# - 基于 WPF 数据网格中的列值显示图像

javascript - 在 javascript Google Analytics 中使用 C# 变量,变量范围

c# - 如何删除文本文件中不是大于特定值的数字的所有行? (包括字符串)

c# - 将对象分配给c#中的treeview子节点以识别父节点

c# - TreeView 详细架构问题

c# - 如何从 MVVM 中的 ViewModel 选择数据网格中的一行?

c# - 在 C# 作用域外声明变量

javascript - 从数据库生成 Google map 标记

C# 继承 : modify base class variable from derived class