0

Please see the database DDL below:

create table datetest (datetime1 datetime)
insert into datetest values(getdate())

Please see the presentation code below:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
    DataSourceID="SqlDataSource1">

     <Columns>
            <asp:TemplateField HeaderText="Status">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# bind("datetime1") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>

    </asp:GridView>

The output on the page is: 11/04/2013 19:51:40, but the database value is: 2013-04-11 19:51:40.017 (notice the milliseconds are chopped off the first value).

I have looked into this and have found a number of posts on here, which suggest using EVAL and String.Format, but I have been unsuccessful. How can I display milliseconds on the webpage?

I have tried the following code:

<asp:Label ID="Label1" runat="server" Text='<%
Eval("datetime1").ToString("dd/MM/yyyy hh:mm:ss.fff") %>'></asp:Label>

However, I am prompted with an exception: "Conversion from string "dd/MM/yyyy" to type 'Integer' is not valid."

4

2 回答 2

1
<asp:Label ID="Label1" runat="server" Text='<%# Eval("datetime1").ToString("dd/MM/yyyy hh:mm:ss.fff") %>'></asp:Label>

Something like that should do it. If you need help with custom time formats, have a look here: http://msdn.microsoft.com/en-gb/library/8kb3ddd4.aspx

You do not need to do a template to change the format though, you can add {0:dd/MM/yyyy hh:mm:ss.fff} in the custom format property

于 2013-04-11T19:18:17.617 回答
0

You should use Eval with additional format parameter instead of Eval().ToString() call:

<asp:Label ID="Label1" runat="server"
           Text='<%# Eval("datetime1", "{0:dd/MM/yyyy hh:mm:ss.fff}") %>'></asp:Label>
于 2013-04-11T20:04:16.270 回答