2016-10-04 1 views
1

Comment puis-je définir la précision d'une colonne de données contenant des nombres doubles, flottants ou décimaux dans un System.Windows.Forms.DataGrid?Définir la précision pour double/float/decimal dans System.Windows.Forms.DataGrid

Pour DataGridView, il existe How to format a column with number decimal with max and min in DataGridView?, par exemple.

Je veux 0.0100000001 pour être affiché comme 0.01, par exemple, ce qui serait une précision de 2 chiffres après la marque décimale. Je veux les éviter de ressembler à celui-ci où float et double utilisation de la notation scientifique:

enter image description here

Le code que je l'habitude de remplir le tableau est le suivant:

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; 

Note: Je sais DataGrid est obsolète, mais j'ai affaire à du code existant, s'il vous plaît ne pas commenter pour me dire d'utiliser DataGridView - cela ne m'aide pas.

+0

Voulez-vous changer la précision réelle des valeurs, ou le format dans lequel ils sont affiché? Dans les deux cas, quel résultat recherchez-vous? – stuartd

+0

@stuartd Je veux formater comment ils sont affichés. La source de données elle-même ne doit pas être modifiée. Je cherche un équivalent de http://stackoverflow.com/questions/11229590/how-to-format-a-column-with-number-decimal-with-max-and-min-in-datagridview – sashoalm

+0

Donc vous peut [obtenir une référence à la colonne et définir le format] (http://www.thescarms.com/dotnet/ColumnStyles.aspx)? – stuartd

Répondre

0

S'il vous plaît trouver Mise à jour 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 }); 

toujours premier DataGrid de liaison puis formatez la colonne

+0

Comment cela répond-il à ma question? Ou est-ce censé être un commentaire? Que signifie "ans" dans "Ans mis à jour"? – sashoalm

+0

pl regarder la différence dans le code –

+0

Le code ne change rien. Cela ne change pas la précision et ne répond pas à ma question. En outre, je ne formate pas les colonnes, je définis le DataType qui est une propriété DataTable et ne dépend pas du DataGrid. Votre changement est inutile. – sashoalm

1

J'ai tirais ma solution de commentaires @stuartd. Je devais définir la colonne Formatof the current table style pour le DataGrid.

/// <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); 
    } 
} 

Ensuite, nous pouvons simplement itérer les GridColumnStyles et définissez la propriété Format pour chaque colonne numérique:

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