Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 329
user175084 Avatar asked Dec 29 '22 10:12

user175084


1 Answers

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>
like image 117
Canavar Avatar answered Jan 11 '23 21:01

Canavar