Posted by : Anwar Hossain
Category : How to use grid view in asp.net c#

Gridview datetime format example in ASP.Net c#

Dear viewer I will show how to set date time field with different format style at the time of using ASP.Net Gridview Control . Sometimes we need to use gridview datetime with different format .For Showing date with different formats in ASP.Net gridview we have to use DataFormatString. I have used two gridview to show all kinds of datetime format style. I have used here sample local data but real time data will be retrieved from from database.

ASP.Net Gridview datetime format example

HTML

        <asp:GridView ID="Studentlist" CssClass="Grid" runat="server" AutoGenerateColumns="False"

            CellPadding="4" ForeColor="#333333" GridLines="None" Width="53%" Font-Size="10pt">

            <AlternatingRowStyle BackColor="White" />

            <Columns>

                <asp:BoundField DataField="JoingDate1" HeaderText="Month and year {0:y} "  HtmlEncode="false" DataFormatString="{0:y}" />

                <asp:BoundField DataField="JoingDate2" HeaderText="Short Date {0:d}"  HtmlEncode="false" DataFormatString="{0:d}" />

                <asp:BoundField DataField="JoingDate3" HeaderText="Long Date {0:D}"  HtmlEncode="false" DataFormatString="{0:D}" />

                <asp:BoundField DataField="JoingDate4" HeaderText="Full date and time except sec {0:f} "

                    DataFormatString="{0:f}" />

                <asp:BoundField DataField="JoingDate5" HeaderText="Full date {0:F} "  HtmlEncode="false" DataFormatString="{0:F}" />

                <asp:BoundField DataField="JoingDate6" HeaderText="Date time except sec {0:g}"  HtmlEncode="false" DataFormatString="{0:g}" />

            </Columns>

            <EditRowStyle BackColor="#7C6F57" />

            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

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

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

            <RowStyle BackColor="#E3EAEB" />

            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />

            <SortedAscendingCellStyle BackColor="#F8FAFA" />

            <SortedAscendingHeaderStyle BackColor="#246B61" />

            <SortedDescendingCellStyle BackColor="#D4DFE1" />

            <SortedDescendingHeaderStyle BackColor="#15524A" />

        </asp:GridView>

 

        <asp:GridView ID="grdDataFormatingExm" CssClass="Grid" runat="server" AutoGenerateColumns="False"

            CellPadding="4" ForeColor="#333333" GridLines="None" Width="53%" Font-Size="10pt">

            <AlternatingRowStyle BackColor="White" />

            <Columns>

                <asp:BoundField DataField="JoingDate1" HeaderText="Full date {0:G}"  HtmlEncode="false" DataFormatString="{0:G}" />

                <asp:BoundField DataField="JoingDate2" HeaderText="Month and day {0:M}"  HtmlEncode="false" DataFormatString="{0:M}" />

                <asp:BoundField DataField="JoingDate3" HeaderText="Short time {0:t}"  HtmlEncode="false" DataFormatString="{0:t}" />

                <asp:BoundField DataField="JoingDate4" HeaderText="Long Time {0:U} "  HtmlEncode="false"

                    DataFormatString="{0:U}" />

                <asp:BoundField DataField="JoingDate5" HeaderText="universal time {0:u}"  HtmlEncode="false" DataFormatString="{0:u}" />

                <asp:BoundField DataField="JoingDate6" HeaderText="Full date with GMT {0:R}"  HtmlEncode="false" DataFormatString="{0:R}" />

            </Columns>

            <EditRowStyle BackColor="#7C6F57" />

            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />

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

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

            <RowStyle BackColor="#E3EAEB" />

            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />

            <SortedAscendingCellStyle BackColor="#F8FAFA" />

            <SortedAscendingHeaderStyle BackColor="#246B61" />

            <SortedDescendingCellStyle BackColor="#D4DFE1" />

            <SortedDescendingHeaderStyle BackColor="#15524A" />

        </asp:GridView>

 

CODE BEHIND

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Data;

 

public partial class GridviewDataFormating : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

 

        Session["StudentList"] = GetStudentList();

        Studentlist.DataSource = Session["StudentList"];

        Studentlist.DataBind();

 

        grdDataFormatingExm.DataSource = Session["StudentList"];

        grdDataFormatingExm.DataBind();

 

 

    }

  

    static DataTable GetStudentList()

    {

 

        //Created virtual data Storage

 

        DataTable table = new DataTable();

 

        table.Columns.Add("JoingDate1", typeof(DateTime));

        table.Columns.Add("JoingDate2", typeof(DateTime));

        table.Columns.Add("JoingDate3", typeof(DateTime));

        table.Columns.Add("JoingDate4", typeof(DateTime));

        table.Columns.Add("JoingDate5", typeof(DateTime));

        table.Columns.Add("JoingDate6", typeof(DateTime));

        table.Columns.Add("JoingDate7", typeof(DateTime));

        table.Columns.Add("JoingDate8", typeof(DateTime));

 


       // Now have created some row to fill the the data table


 

        table.Rows.Add("11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010",

"11/5/2010", "11/5/2010");

        table.Rows.Add("11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010",

"11/5/2010", "11/5/2010");

        table.Rows.Add("11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010",

"11/5/2010", "11/5/2010");

        table.Rows.Add("11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010",

"11/5/2010", "11/5/2010");

        table.Rows.Add("11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010",

"11/5/2010", "11/5/2010");

        table.Rows.Add("11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010",

"11/5/2010", "11/5/2010");

        table.Rows.Add("11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010", "11/5/2010",

"11/5/2010", "11/5/2010");

 

        return table;

    }

 

  

}

 

Out Put

grid_view_data_time_formating
grid_view_data_time_formating_example



Realted Article Headline

Gridview datetime format example in ASP.Net c#
Displaying total column value in the Gridview Footer using asp.net c#.
How to create grid view paging in asp.net using c#
How to bind Multi-Dimensional Arrays Data to gird view in asp.net using C#
How to bind Two-Dimensional Arrays Data to gird view in asp.net using C#
How to bind Single-Dimensional Arrays Data to gird view in asp.net using C#
how to export gridview data to pdf in asp.net c#
how to export gridview data to word in asp.net using c#
how to export gridview data to excel in c#
How to delete row from grid view showing confirmation JavaScript using asp.net c#
How to delete data from grid view using row deleting event in asp.net c#
How to bind data into gridview using code behind in asp.net c#
How to scroll gird view overflow data in asp.net c#
How to Bind data into grid view control using asp.net c#

Article Category

How to create asp.net control dynamically
Learn HTML for beginner
DataList example in C Sharp
Mail sending in asp.net c#
State Management in ASP C #
Basic sql tutorial for Beginner
DataTable example in ASP.Net C#
How to use LINQ in ASP.NET C#
asp.net c # basic tutorial
How to use ajax toolkit in asp.net C#
How to use different types of validation control using asp.net c#
How to use grid view in asp.net c#
Protected by Copyscape Online Plagiarism Detection