Question

I have a data table and some of the columns in this data table having values of type double.

Everything is fine up to here, but when I get the value as 0.0000005 it's displaying some 5E-9 exponential value.

I need to display same value instead of exponential.

I applied culture info like this but it couldn't help me:

NumberFormatInfo numberFormat = new NumberFormatInfo();
numberFormat.NumberGroupSeparator = string.Empty;
numberFormat.NumberDecimalDigits = 9;
CultureInfo info = new CultureInfo(CultureInfo.CurrentCulture.Name);
info.NumberFormat = numberFormat;

//data table having data
targetTable.Locale=info;

Can one help me out?

Thanks,

Mohan

Était-ce utile?

La solution

The documentation for DataTable.Locale doesn't say anything about setting number formats; it says:

Gets or sets the locale information used to compare strings within the table.

So it may not do what you're expecting it to do.

If you're auto-generating columns you may need to store the values as strings in the DataTable instead so you can specify the formatting. Another option is to attach to the AutoGeneratingColumn event to set custom column properties:

excerpt from MSDN :

private void dataGrid1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    // Modify the header of the Name column.
    if (e.Column.Header.ToString() == "Name")
        e.Column.Header = "Task";
    // Replace the DueDate column with a custom template column.
    if (e.PropertyName == "DueDate")
    {
        // Create a new template column.
        DataGridTemplateColumn templateColumn = new DataGridTemplateColumn();
        templateColumn.Header = "Due Date";
        templateColumn.CellTemplate = (DataTemplate)Resources["dueDateCellTemplate"];
        templateColumn.CellEditingTemplate = (DataTemplate)Resources["dueDateCellEditingTemplate"];
        templateColumn.SortMemberPath = "DueDate";
        // ...
        // Replace the auto-generated column with the templateColumn.
        e.Column = templateColumn;
    }

    // Cancel AutoGeneration of all boolean columns.
    if (e.PropertyType == typeof(bool))
        e.Cancel = true;

    if (e.PropertyName == "ValueField")
    {
        (e.Column as DataGridTextColumn).Binding.StringFormat = "N5";
    }
}

I will say that auto-generating columns is a nice way to quickly prototype a grid but I usually end up directly configuring the columns to specify number formats, etc.

Autres conseils

With a Windows Forms DataGrid, you can use the technique described on How to: Format Data in the Windows Forms DataGridView Control on MSDN.

For example:

dataGridView1.Columns["YourColumn"].DefaultCellStyle.Format = "F9";

for fixed number of decimal digits after the decimal separator.

To see what "F9" means and what other strings are valid, read Standard Numeric Format Strings and Custom Numeric Format Strings. Simmilar format strings exist for DateTime, TimeSpan, enum types, and so on.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top