2016-10-04 28 views
1

如何设置保存System.Windows.Forms.DataGrid中double,float或decimal数据的数据列的精度?在System.Windows.Forms.DataGrid中设置double/float/decimal的精度

对于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 - 它不会帮助我。

+0

你想改变这些值的实际精度,或格式,其中他们显示?无论哪种情况,你在寻找什么结果? – stuartd

+0

@stuartd我想格式化它们的显示方式。数据源本身不应该改变。我正在寻找相当于http://stackoverflow.com/questions/11229590/how-to-format-a-column-with-number-decimal-with-max-and-min-in-datagridview – sashoalm

+0

所以,你可以[获得对列的引用并设置格式](http://www.thescarms.com/dotnet/ColumnStyles.aspx)? – stuartd

回答

0

请查找更新ANS

var table = new DataTable(); 
table.Columns.Add("double"); 
table.Columns.Add("float"); 
table.Columns.Add("decimal"); 
dataGrid1.DataSource = table; 
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 }); 

总是先绑定的DataGrid然后格式化列

+0

这是如何回答我的问题?还是意味着评论? “Updated ans”中的“ans”是什么意思? – sashoalm

+0

pl看看代码中的差异 –

+0

代码不会改变任何东西。它不会改变精度,也不会回答我的问题。此外,我没有格式化列,我设置DataType这是一个DataTable属性,并不依赖于DataGrid。你的改变是毫无意义的。 – sashoalm

1

我来源于@stuartd评论我的解决方案。我需要为DataGrid设置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并设置格式属性为每个数字列:

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"; 
}