C# 在gridview中格式化十进制值

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

format decimal value in gridview

c#asp.netgridview

提问by user175084

I have a bound field in my Gridview which is getting its value from a database table.

我的 Gridview 中有一个绑定字段,它从数据库表中获取其值。

I have got the data but don't know how to format it inside the gridview.

我已经获得了数据,但不知道如何在 gridview 中对其进行格式化。

For example I get total data from below like "123456", but I want to display as "123,456"

例如,我从下面获得总数据,如“123456”,但我想显示为“123,456”

  <asp:BoundField DataField="totaldata" HeaderText="Total Data"  
       ReadOnly="True" SortExpression="totaldata" />

How can I do this? Do I need to convert the bound field into a template field ? But what do i do after that.

我怎样才能做到这一点?我需要将绑定字段转换为模板字段吗?但在那之后我该怎么办。

please help.

请帮忙。

I have used DataFormatString="{0:n0}" and it solved the above problem.

我使用了 DataFormatString="{0:n0}" 并解决了上述问题。

how do i do for this:

我该怎么做:

<asp:TemplateField HeaderText="Failed Files" 
            SortExpression="NumFailed">
            <ItemTemplate>
             <asp:Image ID="Image2" runat="server" ImageUrl="~/NewFolder1/warning_16x16.gif" />
                <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx?id="+Eval("MachineID") %>' Text='<%# Bind("NumFailedFiles") %>'></asp:HyperLink>
            </ItemTemplate>
        </asp:TemplateField>

the hyperlink has the number which need to be formatted...

超链接有需要格式化的数字...

采纳答案by Canavar

Use DataFormat property :

使用 DataFormat 属性:

<asp:BoundField DataField="totaldata" HeaderText="Total Data"  
     ReadOnly="True" SortExpression="totaldata" DataFormatString="{0:n3}" />

EDIT :For the second part of your question use Eval method's second parameter to format your data :

编辑:对于您问题的第二部分,请使用 Eval 方法的第二个参数来格式化您的数据:

<%# Eval("NumFailedFiles", "{0:n3}") %>

Then your template will be like that :

那么你的模板将是这样的:

<asp:TemplateField HeaderText="Failed Files" 
    SortExpression="NumFailed">
    <ItemTemplate>
     <asp:Image ID="Image2" runat="server" 
         ImageUrl="~/NewFolder1/warning_16x16.gif" />
        <asp:HyperLink ID="HyperLink1" runat="server" 
                 NavigateUrl='<%# "GetFilesFailed.aspx?id="+Eval("MachineID") %>' 
                 Text='<%# Eval("NumFailedFiles", "{0:n3}") %>'></asp:HyperLink>
    </ItemTemplate>
</asp:TemplateField>

回答by user175084

I think you need to take a look at this MSDN article on How to Format Data in the DataGridView

我认为您需要查看有关如何在 DataGridView 中格式化数据的这篇 MSDN 文章

回答by I Am Back

A couple of ways in how you can do that

有几种方法可以做到这一点

Option 1

选项1

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx?id="+Eval("MachineID") %>' Text='<%# Bind("NumFailedFiles","{0:n0}") %>'></asp:HyperLink>

Option 2

选项 2

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx?  id="+Eval("MachineID") %>' Text='<%# Bind("NumFailedFiles").ToString("N") %>'></asp:HyperLink>

Option 3
Create a method in the code behind page that returns the formatted number.

选项 3
在代码隐藏页面中创建一个返回格式化数字的方法。

 protected string GetFormatedNumber(object number)   
 {  
   if ( number != null )  
       {  
          return number.ToString("N");  
       }  
       return "0";   
 }  

And call the method in your aspx page as below:

并在您的 aspx 页面中调用该方法,如下所示:

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx?  id="+Eval("MachineID") %>' Text='<%#GetFormatedNumber(Eval("NumFailedFiles")) %>'></asp:HyperLink>

回答by chab ratana

if you want to format your data on gridview use "{0:n3}"

如果要在 gridview 上格式化数据,请使用“{0:n3}”

 <asp:Label ID="label" runat="server" Visible="true" Text='<%#DataBinder.Eval(Container.DataItem, "data","{0:n3}") %>'></asp:Label>