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>
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.
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.
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.
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}") %>' />
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**
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With