Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date format without time in ASP.NET Gridview

Tags:

c#

in ASP.NET gridview binding two dates. I want to display dd/MM/yyyy but it displays 10/03/2014 00:00:00.

<asp:TemplateField HeaderText ="Fromdate" >
   <ItemTemplate >
   <asp:Label ID="lblFromDate" runat="server" 
              DataFormatString="{0:dd/MM/yyyy}" 
              HtmlEncode="false"  
              Text='<%# Eval("Fromdate") %>' />
    </ItemTemplate>
</asp:TemplateField>
like image 402
Vineeth S Avatar asked Mar 17 '14 08:03

Vineeth S


People also ask

How to display only date in ASP net GridView?

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 to format date in ASP net GridView?

The HTML Markup consists of an ASP.Net GridView with three BoundField columns. The third BoundField column is bound to a DateTime field and the DataFormatString property is set to {0:dd/MM/yyyy} in order to display the DateTime field value in dd/MM/yyyy format.

How to change date format in GridView ASP net c#?

In some situations we don't want to show complete date and time we need to display only date or only time or we need to display date in different formats and we need to change the currency format display also. To display date with different formats in gridview we need to set DataFormatString and HtmlEncode properties.


2 Answers

DataFormatString is a property of BoundField control and doesn't affect any other control. You can specify the format in an Eval expression:

Text='<%# Eval("Fromdate", "{0:dd/MM/yyyy}") %>' />
like image 119
Yuriy Rozhovetskiy Avatar answered Sep 28 '22 05:09

Yuriy Rozhovetskiy


According to this article on MSDN the DataFormatString attribute has a limited number of variants for DateTime data.

What you are looking for is:

DataFormatString="{0:d}"

which is the short date pattern.

In order to get the dd/MM/yyyy format, you need to also set your culture info properly (for example to **en-GB**).

like image 28
nestedloop Avatar answered Sep 28 '22 03:09

nestedloop