ASP.NET – Format datagrid columns.
There are two ways to modify the format of the data in a Datagrid/GridView.
One is through the design view.
Right click on the Grid View / DataGrid
select Property Builder and Column view, under the selected column
for which you wan to set the format.
Then Under the Bound Column Properties section set the data formatting expression
value to the desired format the selected column.
![]()
The other way to do is to edit the HTML of the page and add the dataformatstring attribute in the BoundColumn tag of the desired column.
The Format expressions are as follows: -
1. This will display a text Price: followed by the numeric text in currency format
Data Format String value : Price: {0:C}
**** ABOVE MENTIONED FORMAT IS USING A ZERO , NOT THE ALPHABET O*****
Applied to numeric and decimal data types.
The currency format is according to the culture info set in the web config file.
2. Integers are displayed in a zero-padded field four characters wide.
Data Format String value : {0:D4}
integer datatype only.
3. To show two decimal places followed by “%”
Data Format String value : {0:N2}%
4. To round the numbers to one decimal place and are zero padded for numbers less than three digits.
Data Format String value : {0:000.0}
Applied to numeric and decimal datatype only.
5. Long Date format
Data Format String value : {0:D}
Applied to date and datetime datatype only.
( the above formatting is as set in the web config)
6. Short Date format
Data Format String value : {0:d}
Applied to date and datetime datatype only.
( the above formatting is as set in the web config)
6. Numeric Date format
Data Format String value : {0:yy-MM-dd}
Applied to date and datetime datatype only.






Its helpful thanks for your effort.
Vijay
Dear Vijayan,
I am new to dot.net world. I am a AS/400 programmer. I have date field in CYYMMDD format in AS/400 database (ex: 1080305, being 03/05/2008 is the actual date). How do I display the date 1080305 in correct format 03/05/2008? Any help is highly appreciated.
Thank you in advance.
Satyabrata
My name is Rana, Vijayan was one my reader.
Here is your answer.
Option 1:
Directly pull from the database and just bound it with the column.
use AdventureWorks
Select Convert(varchar,ModifiedDate,101) from Person.Contact.
Option Two:
In “Protected Sub GridView1_RowDataBound(”
Bound it and use string property
strDATEVAL.String(”dd/MM/yyyy”)
If you need a code for this please write.
I will be writing later, I have to rush and talk to the client.
regards,
RANA
Hi,
I am new to .net and working on the MIS website. I hav one datagrid in my proj, i databind this datagrid by running RFC from SAP. i hav one field where i display MFG date of the product. But when i databind this datagrid i get val of that particular column in “20080712″ format i need to display in 12-07-2008 format. plz could help me out to solve this problem.
Hi Saatya Jeet, From your stored procedure or from your query, return the date column after converting in the required format it would be simplest and easiest.
Select Convert(varchar,getdate(),105)
Convert(Varchar, COLUMN NAME ,105) AS DATECOLUME NAME
Hope it will resolve your problem
Hey I want to format my decimal column within coding.
This is my coding I’m just wanting to knw if anyone can help and make it round to 2 decimal places, even better make it currency.
New DataColumn(”TotalCost”, GetType(Decimal))
Hi Jason, Little late reply appology for that.
There is a way when you are binding data or retrieving from the database you can, make your format in your query, thats the best choice. Client side I wouldnt suggest you to do this.
Suppose.
Declare @price decimal(9,2)
select @price
it would be like this
100.99
this would be the best way.
If you have any problem related to formating please let me know I will make a example.
Thanks
Joggee
thanks for sharing such a useful information
Hi,
I want to change numeric value from database to string values in datagrid. That is, let say, if in database it is “1″, then in datagrid, it should say “qualified” and so on. How can I do that? Please help!
Thanks for writing this great blog I really enjoyed.
http://greeki.ru/news-2.html что за ссылка левая ???
Админ нашел удивительно хорошую партнерку – http://divx-mpeg4.ru/rabota4.html
очень советую пока есть время.
Мой баланс за день – Ваш баланс :$ 17.10 (р. 516.35). Советую по дружески !
если не будет работать зайди позже там щас все сидят нагзука большая
Очень полезная инфа, согласен с автором!