Posts Tagged ‘date dataformatstring using web.cofig’


I was required to bind DataFormatString in my GridView control and I was using  asp:BoundField, so what I have done is, I have set one date format in my web.config file and then access that format in my GridView to bind it in my HTML code.

Write the following code in your web.config file to set your desired date format:



            <add key=GridDateFormat value={0:dd/MM/yyyy}/>



You can apply any date format other than “dd/MM/yyyy” as per your need, now how to access this date format in Grid view? is very important, so below is the code, that defines how to access web.config file’s date format in your GridView:

<asp:BoundField HtmlEncode=”False” DataField=”CreatedDate”  SortExpression=”CreatedDate” HeaderText=”Created Date” DataFormatString=”<%$Appsettings:GridDateFormat%>“/>

Here, we have set “HtmlEncode” property to “False”, if you do not set this property then it might possible that you will get proper result while running your application in localhost but you might not get proper result of the date format after publishing the code on hosting server. Therefore, it is better to set HtmlEncode property to False. So, now no need to apply date format at run-time, you can use your required date format in a GridView while creating it.