Solved

Format Gridview

Posted on 2008-10-03
6
915 Views
Last Modified: 2013-12-17
I have a gridview which has a datetime column which displays dd/mm/yyy mm:hh:ss.

I would like to format the column in the gridview to show only dd/mm/yy is this possible?
0
Comment
Question by:Mr_Shaw
  • 2
  • 2
  • 2
6 Comments
 
LVL 16

Expert Comment

by:Gyanendra Singh
ID: 22632440
You need to set a field HtmlEncode = false when you have a bound data column to a gridview. I created this sample  for you.

Notice the formatting applied on date fields -( DataFormatString="{0:d}" HtmlEncode=false)

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="OrderID"
            DataSourceID="Northwind">
            <Columns>
                <asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False" ReadOnly="True"
                    SortExpression="OrderID" />
                <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" />
                <asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" SortExpression="EmployeeID" />
                <asp:BoundField DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate" DataFormatString="{0:d}" HtmlEncode=false />
                <asp:BoundField DataField="RequiredDate" HeaderText="RequiredDate" SortExpression="RequiredDate" DataFormatString="{0:d}" HtmlEncode=false />
                <asp:BoundField DataField="ShippedDate" HeaderText="ShippedDate" SortExpression="ShippedDate" DataFormatString="{0:d}" HtmlEncode=false />
            </Columns>
        </asp:GridView>


0
 
LVL 4

Expert Comment

by:sijishJohn
ID: 22632461
use this

<asp:boundfield datafield="datecolum" dataformatstring="{0:MM/dd/yy}" htmlencode="false" />
without the htmlencode attribute set to false you will get the time added as well
0
 

Author Comment

by:Mr_Shaw
ID: 22632509
I have used
<asp:boundfield datafield="datecolum" dataformatstring="{0:MM/dd/yy}" htmlencode="false" />

and I get the following error;

Warning 6 Element 'boundfield' is not a known element. This can occur if there is a compilation error in the Web site, or the web.config file is missing.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 4

Expert Comment

by:sijishJohn
ID: 22632568
please provide the html code of your gridview
0
 
LVL 16

Accepted Solution

by:
Gyanendra Singh earned 500 total points
ID: 22632572
.net is case sensitive use  this
<asp:BoundField DataField="datecolum" HeaderText="OrderDate" DataFormatString="{0:MM/dd/yy}" HtmlEncode=false />
0
 

Author Closing Comment

by:Mr_Shaw
ID: 31502699
thanks
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question