Search code examples
c#.netwinformsdatagridviewcell-formatting

Change number group separator in numeric columns of DataGridView


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?


Solution

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