Format Date in a DataGrid

K

Kenneth Keeley

Hi,
I am reading in results from an SQL database one of the fields is a date
value. I wish to be able to display the date as only "dd/mm/yyyy" can some
body give me an example. Here is the code I am using at the moment.

<asp:BoundColumn HeaderText="Date" DataField="InsertDate"
SortExpression="InsertDate" ItemStyle-Width="150" />

Thanks
Kenneth
 
K

Kannan Meiappan

Hi

In the item data bound event,
e.Item.Cells[6].Text =
Convert.ToDateTime(e.Item.Cells[6].Text).ToString("dd-MM-yyyy");

instead of 6 change it your column number....

regards,
Kannan
 
M

Martin Dechev

Hi,

Set the DataFormatString property to whichever format you need. See the
BoundColumn.DataFormatString property topic for details:

http://msdn.microsoft.com/library/e...rolsBoundColumnClassDataFormatStringTopic.asp

See Formatting Overview (specifically the link to "Custom DateTime format
strings") to get more info on formatting:

http://msdn.microsoft.com/library/en-us/cpguide/html/cpconformattingoverview.asp

Example:

<asp:BoundColumn HeaderText="Date" DataField="InsertDate"
DataFormatString="{0:ddd, dd MMM yyyy, HH:mm}"
SortExpression="InsertDate" ItemStyle-Width="150" />

for today, supposing the Thread.CurrentCulture is "en-??" will display

Tue, 12 Oct 2004, 12:55

Hope this helps
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top