2016-10-04 62 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"; 
}