C# 更改 DataGridView 数字列中的数字组分隔符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15633138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:25:25  来源:igfitidea点击:

Change number group separator in numeric columns of DataGridView

c#.netwinformsdatagridviewcell-formatting

提问by Vali Maties

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

我想以这种方式格式化数字单元格1 231 241.45。我试过N2格式选项:

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

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

但是N2格式将逗号而不是空格。我想要空格作为数字组分隔符。是否可以更改号码组分隔符?

采纳答案by Reza Aghaei

To change the number group separator in DataGridViewyou can create a specific culture and then set its NumberFormat.NumberGroupSeparatorto new value, then set DefaultCellStyle.FormatProviderof the DataGridViewto the modified culture:

要更改数字组分隔符,DataGridView您可以创建一个特定的文化,然后将其设置NumberFormat.NumberGroupSeparator为新值,然后将 设置DefaultCellStyle.FormatProviderDataGridView修改后的文化:

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

回答by Freelancer

Refer this link for cellstyle formatting>>

有关单元格样式格式,请参阅此链接>>

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

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

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

D is for integers.

D 代表整数。

回答by Vali Maties

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.

它不是很优雅,但它正在工作。

回答by Ramgy Borja

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

回答by Nayyar Abbas

In the data grid CellFormattingevent do the following:

在数据网格CellFormatting事件中执行以下操作:

private void dg_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (cell.Value is decimal)
    {
        e.CellStyle.Format = "0.##";
    }
}