Solved

Format Gridview

Posted on 2008-10-03
6
910 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with WinAPI Signatures in VB.NET for SetWindowSubclass 2 35
How do ASP.NET and MVC work together? 4 30
Server Error 11 47
ASP.NET 5 Templates 2 68
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

862 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

Need Help in Real-Time?

Connect with top rated Experts

30 Experts available now in Live!

Get 1:1 Help Now