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.

有帮助吗?

解决方案

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}"  />

其他提示

I specifically wanted to format my date as yyyy/MM/dd without the time equivalent to the following:

string dateString = DateTime.Now.ToString("yyyy/MM/dd");

To do this I just added the format on the DataFormatString attribute for the BoundField:

<asp:BoundField DataField="DatePosted" HeaderText="Date" DataFormatString="{0:yyyy/MM/dd}" />
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top