17

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
  • 7,386
  • 9
  • 58
  • 95
gofor.net
  • 3,850
  • 10
  • 37
  • 64

7 Answers7

40

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
  • 40,946
  • 9
  • 129
  • 143
  • 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
  • 1,947
  • 15
  • 24
Jig12
  • 2,479
  • 3
  • 18
  • 27
11

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

Amit Patel
  • 147
  • 1
  • 5
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,020
  • 2
  • 22
  • 34
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
  • 7
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,335
  • 6
  • 27
  • 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
  • 7,386
  • 9
  • 58
  • 95
Code
  • 467
  • 4
  • 8