Click here to Skip to main content
12,697,801 members (27,222 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
this works in vb.net datagridview .Columns[0].DefaultCellStyle.Format = "yy/mm/dd";
but in asp.net there is no "DefaultCellStyle" so how do i do this.
Posted 20-Oct-12 1:29am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
rosoftghana 20-Oct-12 7:05am
   
pls i want it in c sharp
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

You can use "DataFormatString" property in asp:BoundField to format the date fields. Here is the sample code

<asp:gridview id="GridView1" runat="server" xmlns:asp="#unknown">
    <columns>
        <asp:boundfield datafield="JoinDate" dataformatstring="{0: dd-MM-yyyy hh:mm tt }" />
    </columns>
    </asp:gridview>


you can also use the source code to change the date format using Gridviews RowDataBound event. Find the template control for your date and then convert the date to your required format in this event.
  Permalink  
Comments
rosoftghana 20-Oct-12 7:03am
   
pls i want it in c sharp
vivekmishra168 20-Oct-12 7:40am
   
Here is the aspx code
<pre lang="HTML">
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"
onrowdatabound="GridView1_RowDataBound">
<columns>
<%--<asp:BoundField DataField="JoinDate" DataFormatString="{0: dd-MM-yyyy hh:mm tt }" />--%>
<asp:TemplateField HeaderText="Joining date">
<itemtemplate>
<asp:Label ID="lblJoinDate" Text='<%#Eval("joindate") %>' runat="server" />




</pre>

and this is the code behind in c#
<pre lang="c#">
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = createDataSource();
GridView1.DataBind();
}

private DataTable createDataSource()
{
DataTable dt = new DataTable();
dt.Columns.Add("JoinDate", typeof(DateTime));
dt.Rows.Add(DateTime.Now);
return dt;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblJoinDate = (Label)e.Row.FindControl("lblJoinDate");
DateTime joindate = Convert.ToDateTime(lblJoinDate.Text);
lblJoinDate.Text = joindate.ToString("dd-MM-yyyy hh:mm:ss tt");
}
}
</pre>

Hope it will help you
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Here is the aspx code
<pre lang="HTML">
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"
        onrowdatabound="GridView1_RowDataBound">
    <Columns>
        <%--<asp:BoundField DataField="JoinDate" DataFormatString="{0: dd-MM-yyyy hh:mm tt }" />--%>
        <asp:TemplateField HeaderText="Joining date">
            <ItemTemplate>
                <asp:Label ID="lblJoinDate" Text='<%#Eval("joindate") %>' runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    </asp:GridView>
</pre>

and this is the code behind in c#
<pre lang="c#">
protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = createDataSource();
        GridView1.DataBind();
    }

    private DataTable createDataSource()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("JoinDate", typeof(DateTime));
        dt.Rows.Add(DateTime.Now);
        return dt;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblJoinDate = (Label)e.Row.FindControl("lblJoinDate");
            DateTime joindate = Convert.ToDateTime(lblJoinDate.Text);
            lblJoinDate.Text = joindate.ToString("dd-MM-yyyy hh:mm:ss tt");
        }
    }
</pre>

Hope it will help you
  Permalink  
Comments
Atul Gandhale 26-Nov-14 7:57am
   
this solution work for me thanku.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 20 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100