Solved

Format Gridview

Posted on 2008-10-03
6
907 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 4

Expert Comment

by:sijishJohn
Comment Utility
please provide the html code of your gridview
0
 
LVL 16

Accepted Solution

by:
Gyanendra Singh earned 500 total points
Comment Utility
.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
Comment Utility
thanks
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Library to convert HTML to PDF. 8 42
Limiting string to two deciamls 18 30
Calling stored proc in EDMX 11 17
XML & .net 5 14
Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

7 Experts available now in Live!

Get 1:1 Help Now