Gridview decimal format

I have a related field in my Gridview that gets its value from the database table.

I have data, but I don’t know how to format it inside gridview.

For example, I get the full data 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 a linked field to a template field? But what should I do after that.

Please, help.

I used DataFormatString = "{0: n0}" and solved this problem.

How can i do 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 a number to format ...

+4
source share
4 answers

Use the 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 the second Eval method parameter to format your data:

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

Then your template will be like 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='<%# Eval("NumFailedFiles", "{0:n3}") %>'></asp:HyperLink> </ItemTemplate> </asp:TemplateField> 
+8
source

Several ways to do this

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 a table per page that returns a formatted number.

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

And call the method on your aspx page as below:

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

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

0
source

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

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

All Articles