format decimal value in gridview

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.

For example I get total data from below like “123456”, but I want to display as “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.

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…

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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>

Method 2

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>

Method 3

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

Method 4

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>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x