2016-10-04 2 views
1

System.Windows.Forms.DataGrid内の倍数、浮動小数点または10進数を保持するデータ列の精度を設定するにはどうすればよいですか?System.Windows.Forms.DataGridのdouble/float/decimalの精度を設定します。

DataGridViewには、たとえばHow to format a column with number decimal with max and min in DataGridView?があります。

たとえば、0.0100000001は、小数点以下2桁の精度である0.01と表示されます。私は彼らが、このように見える避けたいところ科学的表記法の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

can [列への参照を取得し、フォーマットを設定する](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 }); 

は、常に最初のバインドデータグリッドは、列Iは@stuartdコメントから私の解決策を導出しました

+0

これはどのように私の質問に答えますか?それとも、コメントになるのですか? 「更新されたans」の「ans」はどういう意味ですか? – sashoalm

+0

plコードの違いを見てください –

+0

コードは何も変わりません。それは精度を変えず、私の質問に答えません。また、私は列の書式を設定するつもりはない、私はDataTableプロパティであり、DataGridに依存しないDataTypeを設定している。あなたの変化は無意味です。 – sashoalm

1

をフォーマットしてください。 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"; 
} 
関連する問題