Click here to Skip to main content
11,581,353 members (59,948 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 0:29am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
rosoftghana at 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 at 20-Oct-12 7:03am
   
pls i want it in c sharp
vivekmishra168 at 20-Oct-12 7:40am
   
Here is the aspx code
<pre lang="HTML">
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"
onrowdatabound="GridView1_RowDataBound">

<%--<asp:BoundField DataField="JoinDate" DataFormatString="{0: dd-MM-yyyy hh:mm tt }" />--%>
<asp:TemplateField HeaderText="Joining date">

<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 at 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
0 OriginalGriff 260
1 Sergey Alexandrovich Kryukov 155
2 virusstorm 114
3 Frankie-C 105
4 RyanDev 84
0 OriginalGriff 1,183
1 Sergey Alexandrovich Kryukov 825
2 Abhinav S 597
3 F-ES Sitecore 500
4 Suvendu Shekhar Giri 433


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 20 Oct 2012
Copyright © CodeProject, 1999-2015
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