asp.netdatetimegridvieweditboundfield

Gridview BoundField Remove Time from DateTime In Edit/Update Mode


I have the following GridView:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
    DataKeyNames="EVENT,TERM_CODE" DataSourceID="OracleDataSource" ShowHeaderWhenEmpty="True"
    EnableViewState="False">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="EVENT" HeaderText="Event" ReadOnly="True" 
            SortExpression="EVENT" />
        <asp:BoundField DataField="TERM_CODE" HeaderText="Term Code" ReadOnly="True" 
            SortExpression="TERM_CODE" />
        <asp:BoundField DataField="SNAPSHOT_DATA_DATE" HeaderText="Snapshot Date" 
            SortExpression="SNAPSHOT_DATA_DATE" DataFormatString="{0:d}"  />
    </Columns>
</asp:GridView>

The SNAPSHOT_DATA_DATE column is a DateTime field in the database.
I only want to show the date not both the date and time ex. 7/25/2012
So I added DateFormatString="{0:d}" to the BoundField and this works great when viewing the Gridview. However, if I click the edit link to update the date it displays as: 7/25/2012 12:00:00 AM The DateFormatString is ignored in edit mode. Can someone please tell me how to remove the time when in edit / update mode? I don't want users dealing with the time only the date.


Solution

  • Make sure you have set the following properties for formatting to work in Edit Mode in Bound field.

    DataFormatString = ({0:d})
    ApplyFormatInEditMode = True
    

    In your code if you just modify like below, it should work fine

    <asp:BoundField DataField="SNAPSHOT_DATA_DATE" HeaderText="Snapshot Date" ApplyFormatInEditMode="true" SortExpression="SNAPSHOT_DATA_DATE" DataFormatString="{0:d}"  />