20

I have one GridView in a .aspx page, and am showing dynamic data in this grid, setting AutoGenerateColumns="True".

Depending upon which of the options the user selects in a combo box, I am binding different DataTables to the GridView. For example, if the user selects Persons then I am fetching the Persons DataTable, and if the user selects Products then I am fetching Products DataTable.

How can I show a float or double number in 2 decimal places in GridView?

James Skemp
  • 8,018
  • 9
  • 64
  • 107
gofor.net
  • 4,218
  • 10
  • 43
  • 65

7 Answers7

45

The bound column should have a DataFormatString column. You could do something like:

DataFormatString="{0:0.00}" Numeric Custom Format Strings

UPDATE In the case of AutoGenerateColumns="true"... I'd have to know more specifics about what you're binding, but here are some avenues to explore:

  1. I'm not sure if GridView will respect the DataFormatAttribute in Data Annotations. If you are binding an object, and GridView respects that attribute, that might be one route to go.
  2. Wire the RowDataBound event and inspect each column for potential decimal values, and format that way.
moribvndvs
  • 42,191
  • 11
  • 135
  • 149
  • ok,i am binding DataTable to the gridview.Actually i am binding different DataTable to the same gridview according to Conditions that user selected,means suppose i have two options in combo box 1.Persons and 2.Products so if user select the Persons then i am fetching Persons datatable and bind it to GridView and if user selects Products then i am fetching Products datatable and binding it to GridView. – gofor.net Oct 15 '10 at 11:39
  • 1
    Seems like that might be more trouble than it's worth. Can you have two GridViews, one configured the way you want for Persons, and the other for Products, and just hide one and show the other as necessary? If not, you can still probably inspect each column on RowDataBound and format the value however you'd like. – moribvndvs Oct 15 '10 at 12:04
  • Thanks HackedByChinese,actually i have 8 options in combo. :) but thank you for u r instant reply. – gofor.net Oct 15 '10 at 12:20
13

you can write BoundField in GridView:

<asp:BoundField DataField="amount" DataFormatString="{0:n}" />

you can also write TemplateField in GridView

<asp:TemplateField>
  <ItemTemplate>
    <%#Eval("amount","{0:n}")%>
  </ItemTemplate>
</asp:TemplateField>
CoOl
  • 2,637
  • 21
  • 24
Jig12
  • 2,977
  • 4
  • 23
  • 28
  • 1
    m trying something like this - '> and it is displaying all values as 2. where i am wrong? – Silver Sep 20 '16 at 07:25
  • I suspect you need to use 0:n instead of 0:2(?). From DataFormatStrings doc "N or n Displays numeric values in number format (including group separators and optional negative sign). You can specify the number of decimal places." – Zeek2 Aug 24 '23 at 09:29
  • But how do you specify number of decimal places in the above method?? – Zeek2 Aug 24 '23 at 09:31
  • Aah, thusly, put the number of places afterwards (e.g. for Silver 0:n2. From DataFormatStrings doc: https://learn.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.boundfield.dataformatstring?view=netframework-4.8.1 Format: {0:N4} 1234.567 -> 1,234.5670 – Zeek2 Aug 24 '23 at 09:33
  • :( No, sorry that didn't work, I see a similar problem to user Silver. I tried using: Eval("Lat", "0:N4") but just see "0:N4" displayed in my gridview column :( Aah I'm missing the curly braces -- perhaps that is the problem?? – Zeek2 Aug 24 '23 at 09:59
  • :( Not adding curly braces only partially fixed it: it returned to displaying all decimal places rather than just 4 decimal places. This is what I tried: <%# Eval("Lat", "{0:N4}") %>this time, and also: <%# Bind("Lat", "{0:N4}") %> - but didn't re-format the data for either :( – Zeek2 Aug 24 '23 at 10:10
12

You can do DataFormatString="{0:n2}"in your boundfield

Amit Patel
  • 155
  • 1
  • 6
2

This works on a template column, say if you want a decimal out to two places for a ratio (like 1:3)

<%# Eval("somedatacolumn", "1:{0:.##}").ToString() %>
infocyde
  • 4,140
  • 2
  • 25
  • 36
1

If you use DataFormatString and it does not seem to be doing the trick, add HtmlEncode = "false", for example:

<asp:BoundField DataField="DateScheduled" HeaderText="Date Created"   DataFormatString="{0:D}" HtmlEncode="false"/> // date format
<asp:BoundField DataField="Amount" HeaderText="Pay This Amount" DataFormatString="{0:F}" HtmlEncode="false"/> // number format
Jason_P
  • 58
  • 1
  • 1
  • 9
  • Sadly didn't work for me (using VS2019) : DataFormatString="{0:f4}" HtmlEncode = "false" Text='<%# Bind("Lat", "{0:f4}") %>' – Zeek2 Aug 29 '23 at 07:57
  • FYI The above is for a label control in an section. (i.e. Not an asp.Boundfield section, perhaps that make the difference? :( ) – Zeek2 Aug 29 '23 at 08:16
0

There are two easy ways to format things in a GridView. The first is given in a previous answer - use the DataFormatString. The second, which sounds like it applies to your situation, where you are dynamically loading the grid, is to change the data going into the grid.

So, rather than returning a number and trying to format it, return a formatted number and let the GridView display it.

Jason Berkan
  • 8,734
  • 7
  • 29
  • 39
0
<asp:TemplateField HeaderText="Prev Salary" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
        <asp:Label ID="lblGrdSalary" runat="server" Text='<%#Bind("Salary", "{0:n}") %>'></asp:Label>
    </ItemTemplate>
    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    <ItemStyle HorizontalAlign="Center" Width="70px" />
</asp:TemplateField>
James Skemp
  • 8,018
  • 9
  • 64
  • 107
Code
  • 679
  • 5
  • 9