Question

I want to format cells that are numeric in this way 1 231 241.45. I've tried N2 format option:

datagridview1.Columns["col1"].DefaultCellStyle.Format = "N2";

But N2 format puts comma instead of space. I want space as number group separator. Is it possible to change number group separator?

Was it helpful?

Solution 2

To change the number group separator in DataGridView you can create a specific culture and then set its NumberFormat.NumberGroupSeparator to new value, then set DefaultCellStyle.FormatProvider of the DataGridView to the modified culture:

var culture = CultureInfo.CreateSpecificCulture("en-GB");
culture.NumberFormat.NumberGroupSeparator = " ";
dataGridView1.DefaultCellStyle.FormatProvider = culture;
dataGridView1.Columns[1].DefaultCellStyle.Format = "N2";

OTHER TIPS

I managed to make it work with the following code:

string NRFormat="### ### ##0.00"
datagridview1.Columns["col1"].DefaultCellStyle.Format = NRFormat;
datagridview1.Columns["col2"].DefaultCellStyle.Format = NRFormat;

It's not very elegant, but it's working.

Refer this link for cellstyle formatting>>

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

this.dgv_PreviewGrid.DefaultCellStyle.Format = "D4";

D is for integers.

you may try this one.

//1000001 convert to 1 000 001.00
datagridview1.Columns["col1"].DefaultCellStyle.Format = "### ### ##0.00";

//1000 convert to 1 000.00
datagridview1.Columns["col2"].DefaultCellStyle.Format = "### ### ##0.00"; 

In the data grid CellFormatting event do the following:

private void dg_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (cell.Value is decimal)
    {
        e.CellStyle.Format = "0.##";
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top