问题
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?
回答1:
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";
回答2:
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.
回答3:
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.
回答4:
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";
回答5:
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.##";
}
}
来源:https://stackoverflow.com/questions/15633138/change-number-group-separator-in-numeric-columns-of-datagridview