Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to Format Date in GridView Control

Author: DevASP Team
Download Source Code : 680_FormatDate.zip

In this article I will try to explain you how you can format date in a GridView Control while binding your data to column.

To begin with this application create a new website in ASP.Net 2.0 using C# as code behind and add the following code in aspx page:

 

<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" AutoGenerateColumns="false" GridLines="Horizontal">           

            <FooterStyle BackColor="White" ForeColor="#333333" />

            <RowStyle BackColor="White" ForeColor="#333333" />

            <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />

            <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />

            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />

            <Columns>

                <asp:TemplateField HeaderText="Date" >

                    <ItemTemplate>

                        <asp:Label ID="Date" runat="server" Text='<%# Convert.ToDateTime(Eval("BookingDate")).ToString("dd/yyyy") %>'>asp:Label>

                    ItemTemplate>

                asp:TemplateField>           

            Columns>

        asp:GridView>

 

You will see that I have used the label control and itemtemplate and set its text to bind column and there I have used the Convert.ToDateTime method that will change the value to date time and will format it into the way that we want to be displayed on form. Now on page load add the following code:

 

        SqlConnection objConn = new SqlConnection("Persist Security Info=False;User ID=sa;password=master;Initial Catalog=tempdatabase");

        SqlDataAdapter objAdap = new SqlDataAdapter();

        DataSet ds = new DataSet();

 

        objConn.Open();

 

        objAdap = new SqlDataAdapter("Select BookingDate from Ticket", objConn);

        objAdap.Fill(ds);

        GridView1.DataSource = ds;

        GridView1.DataBind();      

 

        objConn.Close();

 

You need to import the System.Data.SqlClient namespace to use the sql namespace methods.

Article Comments
I have created GridView and assign Datasource at RunTime. I need to format one Column which will have the following data.

Start_date Want to Format as Start_date
-------------- ------------------------------------------
10-Mar-2005 10:50:00 10-03-2005

Discard Time and change the Date Format.

Please anyone tell me how to achieve this.

Posted on 5/10/2007 4:27:25 AM by Jogesh Ch Nayak

hi
i have a problem if you can help me on that, that will be really appreciating. i have a gridview and i have more than on date in it and not sure where excatly the date field will fall in gridview and would like to foramte that date field as mm/dd/yyyy.
Thank you

Posted on 12/11/2007 2:13:16 PM by heena

Thanks, sorted my problem

Posted on 1/18/2010 5:33:29 AM by Svapneel Zumkhawala

@heena : you can use this

Text='<%# Bind("DATE_FIELD", "{0:dd/MM/yyyy}") %>'

Posted on 3/26/2010 11:46:57 PM by montox_007

thnx :)

Posted on 6/21/2010 10:23:46 AM by disha

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Working with Application configuration file in .NET 2.0 using C#.

Disclaimer - Privacy
© 2002-2017 DevASP.net