1

I have a gridview with columns like so:

<asp:TemplateField HeaderStyle-Width="75px">
     <HeaderTemplate>
         <asp:Label ID="lblHM1" Text="Hm1" runat="server"></asp:Label>
     </HeaderTemplate>
     <ItemTemplate>
         <asp:Label ID="lblM1" Text='<%# Eval("m1","{0:#0}")%>' runat="server">
          </asp:Label>
     </ItemTemplate>
</asp:TemplateField>

The numbers in this column are often greater than 1000, so I'd like to format them as such. For example, if the data in this column reads 11359, I'd like it to format the number as 11,359.

I have attempted the following:

<asp:TemplateField HeaderStyle-Width="75px">
     <HeaderTemplate>
         <asp:Label ID="lblHM1" Text="Hm1" runat="server"></asp:Label>
     </HeaderTemplate>
     <ItemTemplate>
         <asp:Label ID="lblM1" Text='<%# Eval("m1","{0:N0}")%>' runat="server">
          </asp:Label>
     </ItemTemplate>
</asp:TemplateField>

But the above generates an exception: Input string was not in a correct format

What am I doing wrong?

  • 1
    Does [this](http://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands-place-for-a-number) help? – kush Mar 11 '13 at 17:13

3 Answers3

3

You could do:

 <asp:Label ID="lblM1" Text='<%# Eval("m1","{0:0,0}")%>' runat="server"></asp:Label>

That should format 11239 as "11.239". The Group Separator would be different depending on your culture.

Take a look to the documentation:

The , custom specifier

and

Standard Numeric Format Strings

Custom Numeric Format Strings

EDIT: By the way, it could be a completely different reason. It could be that you're sending the data in one culture, but .Net it's trying to parse with a different one that's not compatible.

Sergio Rosas
  • 545
  • 12
  • 27
0

I tried your format and it worked for me, thanks :)

just change as follow and try if it works for u.

'>

instead of Eval use DataBinder.EVal.

Nisha
  • 1
0

simply use this-

Text='<%# Eval("m1","{0:0,0}") %>'
Mayank
  • 149
  • 1
  • 10