| Gridview Date Format |
|
It is very common that we need to format the date field in the gridview and we can do this through the use of the DataFormatString attributes for date format. <asp :GridView ID="GridView1" runat="server"> <columns> <asp :BoundField DataField="UpdatedOn" DataFormatString="{0:M-dd-yyyy}" HtmlEncode="false" HeaderText="Updated On" /> </columns> </asp> You will need to set the HtmlEncode to false otherwise you will not get the expected result. By default HtmlEncode is set to true to prevent cross-site script attacks and any unwanted content to be shown on the screen. When possible we should always have HtmlEncode set to true. <asp :GridView ID="gvResult" runat="server" > <columns> <asp :TemplateField HeaderText="Updated On" > <edititemtemplate> <asp :Label ID="lblUpdatedOn" runat="server" Text='<%# Eval("UpdatedOn", "{0:M-dd-yyyy}") %>'> </asp> </edititemtemplate> <itemtemplate> <asp :Label ID="lblUpdatedOn" runat="server" Text='<%# Bind("UpdatedOn", "{0:M-dd-yyyy}") %>'> </asp> </itemtemplate> </asp> </columns> </asp>
|