Vali Maties
Vali Maties

Reputation: 399

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?

Upvotes: 3

Views: 25620

Answers (5)

Nayyar Abbas
Nayyar Abbas

Reputation: 61

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

Upvotes: 0

Reza Aghaei
Reza Aghaei

Reputation: 125187

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

Upvotes: 4

Ramgy Borja
Ramgy Borja

Reputation: 2458

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

Upvotes: 0

Vali Maties
Vali Maties

Reputation: 399

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.

Upvotes: 4

Freelancer
Freelancer

Reputation: 9064

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.

Upvotes: 2

Related Questions