ASP.NET – Format datagrid columns.

Posted by Joggee | Visual Studio ASP.NET | Tuesday 4 September 2007 6:02 am

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.

13 Comments »

  1. Comment by Vijayan — October 30, 2007 @ 9:16 am

    Its helpful thanks for your effort.

    Vijay

  2. Comment by Satyabrata — March 5, 2008 @ 9:01 pm

    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

  3. Comment by codeproject — March 6, 2008 @ 10:20 am

    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

  4. Comment by Saatyajeet — August 8, 2008 @ 6:39 pm

    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.

  5. Comment by Joggee — August 11, 2008 @ 11:45 am

    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

  6. Comment by Jason — August 21, 2008 @ 10:17 am

    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))

  7. Comment by Joggee — September 5, 2008 @ 4:50 pm

    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

  8. Comment by Nilesh Thakur — October 4, 2008 @ 2:04 pm

    thanks for sharing such a useful information

  9. Comment by Shyla — October 17, 2008 @ 8:40 pm

    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!

  10. Comment by Leslie — August 31, 2009 @ 3:31 am

    Thanks for writing this great blog I really enjoyed.

  11. Comment by Греция — November 8, 2009 @ 8:08 am

    http://greeki.ru/news-2.html что за ссылка левая ???

  12. Comment by Партнерка — November 16, 2009 @ 12:26 am

    Админ нашел удивительно хорошую партнерку – http://divx-mpeg4.ru/rabota4.html
    очень советую пока есть время.
    Мой баланс за день – Ваш баланс :$ 17.10 (р. 516.35). Советую по дружески !
    если не будет работать зайди позже там щас все сидят нагзука большая

  13. Comment by Dosug — January 7, 2010 @ 3:03 pm

    Очень полезная инфа, согласен с автором!

RSS feed for comments on this post. TrackBack URI

Leave a comment