Reddy Reddy - 1 year ago 149 Question DataGrid Convert YYYYMMDD to MM/DD/YYYY format

Input data is 20160902 (YYYYMMDD)

I'm trying to display this as MM/dd/yyyy in ASP.NET Data Grid and tried to use DataFormatString="{0:MM/dd/yyyy}" which didn't work.

Can some one help me with this?

Answer Source

I assume that your data field is not a date. It may be a string or an integer. You could make sure that your data is retrieved as a date and format it with DataFormatString:

<asp:BoundColumn DataField="MyDate" DataFormatString="{0:MM/dd/yyyy}" />

If, for some reason, that option is not available, you can show your data field in a TemplateColumn of the DataGrid (replace "MyDate" in the example below by the actual name of your data field):

        <asp:Literal runat="server" Text='<%# DateTime.ParseExact(Eval("MyDate").ToString(), "yyyyMMdd", null).ToString("MM/dd/yyyy") %>' />
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download