Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display only date in GridView when pulling data from a DB? C#

I am pulling data from an access database to show in a GridView control on a ASP.NET project. It works fine but I want to see if I can format the data that is being pulled. Currently any currency is being truncated from xx.xx to just the dollar amounts. Also the dates are displaying mm/dd/yyyy hh/mm/ss AM/PM

I tried editing the database itself to the right values (I set the currency field to "Currency" and the date field to "Short Date" but when I pull that date it still shows them not formatted.

EDIT: Sorry, had to take the code down

Any ideas? Thank you

like image 329
SS113 Avatar asked Jan 31 '14 16:01

SS113


People also ask

How to show only date in GridView c#?

You have to Create a BoundField, set its properties including the property dataformatstring which you can use to set format of the field appear in the GridView, and add it to the GridView control.

How can show only date not time in ASP Net?

Just format the output ToString("mm/dd/yyyy"); to be able to see exactly what you want.

How to show only time in GridView in asp net?

To display only Time use DataFormatString.

What is AllowPaging in GridView?

The paging feature can be used with any data source object that supports the System. Collections. ICollection interface or a data source that supports paging capability. To enable the paging feature, set the AllowPaging property to true . By default, the GridView control displays 10 records on a page at a time.


1 Answers

in the grid view of yours add the property called DataFormatString

DataFormatString examples:


{0:dd MMMM yyyy}    -    gives 24 February 2006
{0:MMM dd}          -    gives Feb 24 (substitue MMM with MMMM for the full month name 
                         instead of abbreviation) 
{0:dd/MM/yy}        -    gives 24/02/06 
{0:dd/MM/yyyy}      -    gives 24/02/2006

Sample Code

<asp:BoundField HeaderText="Date" 
                DataField="SampleDate" 
                DataFormatString="{0:MM/dd/yyyy}"  >

MSDN BoundField.DataFormatString Property

like image 94
Amarnath Balasubramanian Avatar answered Oct 02 '22 21:10

Amarnath Balasubramanian