Click here to Skip to main content
15,886,678 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

 
Share this answer
 
Comments
wizy@2020 20-Oct-12 7:05am    
pls i want it in c sharp
You can use "DataFormatString" property in asp:BoundField to format the date fields. Here is the sample code

ASP.NET
<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.
 
Share this answer
 
Comments
wizy@2020 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
XML
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
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900