GridView: Bind date format in BoundField using your web.config settings

Posted: January 17, 2008 in ASP.NET
Tags: , , ,

 

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:

<configuration>

      <configSections>

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

      </configSections>

</configuration>

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.

Advertisements
Comments
  1. Shamjith says:

    Thanks.It worked for me.

  2. Piyush says:

    This really works nice!

    Thanks!!

  3. Gbr says:

    wow cool thank you

  4. harriere says:

    Cool thing buddy…i was searching this for a long time..thank u…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s