format decimal value in gridview

后端 未结 4 774
刺人心
刺人心 2021-01-03 07:09

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

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

相关标签:
4条回答
  • 2021-01-03 07:30

    A couple of ways in how you can do that

    Option 1

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

    Option 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.

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

    And call the method in your aspx page as below:

    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "GetFilesFailed.aspx?  id="+Eval("MachineID") %>' Text='<%#GetFormatedNumber(Eval("NumFailedFiles")) %>'></asp:HyperLink>
    
    0 讨论(0)
  • 2021-01-03 07:30

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

    0 讨论(0)
  • 2021-01-03 07:35

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

     <asp:Label ID="label" runat="server" Visible="true" Text='<%#DataBinder.Eval(Container.DataItem, "data","{0:n3}") %>'></asp:Label>
    
    0 讨论(0)
  • 2021-01-03 07:41

    Use DataFormat property :

    <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("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>
    
    0 讨论(0)
提交回复
热议问题