hi
a have a gridview with datafield column which is bound to a datetime column.
i want the grid column to show the date when the day is first, like this: 24/2/2009

i tried the following format:

DataFormatString="{0:dd/MM/YYYY}"

but the grid is still shows the date when the month is first.
where was i wrong ?

it is case sensitive, try this :

{0:dd/MM/yyyy}

the total example is this :

Default.aspx :

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="PKID" DataSourceID="SqlDataSource1" 
        EmptyDataText="There are no data records to display.">
        <Columns>
            <asp:BoundField DataField="PKID" HeaderText="PKID" ReadOnly="True" 
                SortExpression="PKID" />
            <asp:BoundField DataField="date" DataFormatString="{0:dd/MM/yyyy}" 
                HeaderText="date" SortExpression="date" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:denemeConnectionString1 %>" 
        DeleteCommand="DELETE FROM [Table1] WHERE [PKID] = @PKID" 
        InsertCommand="INSERT INTO [Table1] ([date]) VALUES (@date)" 
        ProviderName="<%$ ConnectionStrings:denemeConnectionString1.ProviderName %>" 
        SelectCommand="SELECT [PKID], [date] FROM [Table1]" 
        UpdateCommand="UPDATE [Table1] SET [date] = @date WHERE [PKID] = @PKID">
        <DeleteParameters>
            <asp:Parameter Name="PKID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter DbType="Date" Name="date" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter DbType="Date" Name="date" />
            <asp:Parameter Name="PKID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    </form>
</body>
</html>

Default.aspx.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

i tryed it the way you suggested with small letters:
DataFormatString="{0:dd/MM/yyyy}"

it still doesnot work.

my full code line is:

<asp:BoundField DataField="date" DataFormatString="{:dd/MM/yyyy}"  HeaderText="date" />

The problem is the by default, the HtmlEncode property of the boundfield attribute is set to True. This helps prevent cross-site scripting attacks and malicious content from being displayed. Microsoft recommends that the HtmlEncode attribute be enabled whenever possible.

The problem is that if this field is enabled, you can not pass format information to the boundfield control. To solve this issue simply set HtmlEncode to false

Try this:

<asp:BoundField DataField="date" DataFormatString="{0:dd/MM/yyyy}" HtmlEncode="false"  HeaderText="date" />

well i tryed this and it still doesnot work.

Still not resolved??????????

just try this:

{d:0}

Works for me!

well i tryed this and it still doesnot work.

Hi

try this example:

<asp:BoundField DataField=”date_from” HeaderText=”DateApply” 
     HeaderStyle-Width =”350px” DataFormatString=”{0:MM/dd/yyyy}” HtmlEncode=”false”>

//—–another way

<asp:TemplateField HeaderText=”Date Submission” SortExpression=”DateSubmission”>
                <ItemTemplate>
                    <asp:Label ID=”Label1″ runat=”server” Text=’<%# Bind(”DateSubmission”, “{0:dd/MM/yyyy}”) %>’></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign=”Center” />
            </asp:TemplateField>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.