views:

867

answers:

3

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

A: 

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

0A0D
+3  A: 

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>
Canavar
this is correct but i just modified a little bit DataFormatString="{0:n0}" just to remove the decimal point....but i have a problem please help i have added it in my question
please help with the above
+1  A: 

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>
I Am Back