C# 如何在gridview中格式化数字字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/314549/
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-03 22:49:30  来源:igfitidea点击:

How to format a number string in gridview?

c#asp.netdata-bindinggridview

提问by

In C#

在 C# 中

I have a processing time number data column in the database which is in in this format "###" or "##" ( eg: "813" or "67")

我在数据库中有一个处理时间编号数据列,格式为“###”或“##”(例如:“813”或“67”)

When I bind it to the grid view I wanted to display it in this format "0.###" (eg: "0.813" or "0.067")

当我将它绑定到网格视图时,我想以这种格式“0.###”(例如:“0.813”或“0.067”)显示它

I tried using {0:0.000} and other formatings. But none seem to work. Can anyone tell me how to write the format string?

我尝试使用 {0:0.000} 和其他格式。但似乎没有一个工作。谁能告诉我如何编写格式字符串?

回答by Greg

You need to disable HTML encoding on that column for the format string to take effect.

您需要在该列上禁用 HTML 编码才能使格式字符串生效。

Further Reading

进一步阅读

回答by Mark Brackett

You should really multiply it by .001 before displaying it, then you can use the normal {0:0.000}format string.

您应该在显示之前将其乘以 0.001,然后您可以使用正常的{0:0.000}格式字符串。

If, for some reason, that's not possible - you can use a format string of {0:0\\.000}which uses the "." not as a decimal separator, but as a literal.

如果由于某种原因,这是不可能的 - 您可以使用{0:0\\.000}使用“。”的格式字符串。不是作为小数分隔符,而是作为文字。

回答by Greg

If for some reason you can't change the value before you bind it to the grid, you can handle the RowDataBound event and divide the number by 1000 before displaying it.

如果由于某种原因无法在将值绑定到网格之前更改该值,则可以处理 RowDataBound 事件并将数字除以 1000,然后再显示它。

// Handle the grid's RowDataBound event
MyGridView.RowDataBound += new GridViewRowEventHandler(MyGridView_RowDataBound);

// Set the value to x / 1000 in the RowDataBound event
protected void MyGridView_RowDataBound( object sender, GridViewRowEventArgs e )
{
    if( e.Row.RowType != DataControlRowType.DataRow )
      return;

    // Cast e.Row.DataItem to whatever type you're binding to
    BindingObject bindingObject = (BindingObject)e.Row.DataItem;

    // Set the text of the correct column.  Replace 0 with the position of the correct column
    e.Row.Cells[0].Text = bindingObject.ProcessingTime / 1000M;
}

回答by avgbody

So are you looking for something like this?

所以你在寻找这样的东西吗?

String.Format("{0:0.000}", test/1000);

回答by avgbody

Do you mean?

你的意思是?

GridView.DataSource = .... 

BoundField total = new BoundField();
total.DataField = "Total";
total.HeaderText = "Total Amount";
total.DataFormatString = "{0:C}";
donations.Columns.Add(total);

......

回答by Vinod Kumar

Nice post, it really helped. But better to do it this way:

不错的帖子,真的很有帮助。但最好这样做:

try
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (((DataRowView)(e.Row.DataItem))["Columnname"].ToString().Equals("Total", StringComparison.OrdinalIgnoreCase))
        {
            e.Row.Font.Bold = true;
            //-----or ant thing you want to do------
        }
    }
}
catch (Exception ex)
{

}

回答by Joe

This can be done using the "," Custom Specifier, which can be used as a number scaling specifier:

这可以使用"," 自定义说明符来完成,它可以用作数字缩放说明符:

Number scaling specifier: If one or more commas are specified immediately to the left of the explicit or implicit decimal point, the number to be formatted is divided by 1000 for each comma.

数字缩放说明符:如果在显式或隐式小数点的左边紧接指定一个或多个逗号,则每个逗号将要格式化的数字除以 1000。

In your example, the format string {0:0,.000}can be used to format 813 as 0.813.

在您的示例中,格式字符串{0:0,.000}可用于将 813 格式化为0.813.

Two commas will scale by 1,000,000, e.g. {0:0,,.0M}will format 1753456 as 1.8M.

两个逗号将按 1,000,000 缩放,例如{0:0,,.0M}将 1753456 格式化为1.8M.