views:

37

answers:

2

Hi Everyone, i have one Gridview in my .aspx page.i am showing dynamic data in this grid.how i will show the float or double number in 2 decimal places in gridview.

thanks in advance

+1  A: 

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.
HackedByChinese
hi,thanks for reply.but i m assigning value to gridview dynamically,and AutoGenerateColumns="True".
gofor.net
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.Productsso 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
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.
HackedByChinese
Thanks HackedByChinese,actually i have 8 options in combo. :)but thank you for u r instant reply.
gofor.net
A: 

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