Time format in GridView

Discussion in 'ASP .Net' started by David C, Aug 20, 2009.

  1. David C

    David C Guest

    I have a Label bound to a Time(0) column data type and want it to display
    hh:mm tt (e.g. 09:15 AM) when the Time column contains 09:00:00 I have the
    Label as below but it displays 09:00:00 and wondered if my format was wrong.
    I have used this on a datetime column before and it worked great. Thanks.

    David

    <asp:Label ID="LblToDoTime" runat="server" Text='<%# Bind("ToDoTime",
    "{0:hh:mm tt}") %>'></asp:Label>
    David C, Aug 20, 2009
    #1
    1. Advertising

  2. David C

    Stan Guest

    On 20 Aug, 19:57, "David C" <> wrote:
    > I have a Label bound to a Time(0) column data type and want it to display
    > hh:mm tt  (e.g. 09:15 AM) when the Time column contains 09:00:00  I have the
    > Label as below but it displays 09:00:00 and wondered if my format was wrong.
    > I have used this on a datetime column before and it worked great.  Thanks.
    >
    > David
    >
    > <asp:Label ID="LblToDoTime" runat="server" Text='<%# Bind("ToDoTime",
    > "{0:hh:mm tt}") %>'></asp:Label>


    You have to use a template field for DateTime formatting. For some
    reason it doesn't work for Bound fields.
    Stan, Aug 21, 2009
    #2
    1. Advertising

  3. David C

    David C Guest

    "Stan" <> wrote in message
    news:...
    On 20 Aug, 19:57, "David C" <> wrote:
    > I have a Label bound to a Time(0) column data type and want it to display
    > hh:mm tt (e.g. 09:15 AM) when the Time column contains 09:00:00 I have the
    > Label as below but it displays 09:00:00 and wondered if my format was
    > wrong.
    > I have used this on a datetime column before and it worked great. Thanks.
    >
    > David
    >
    > <asp:Label ID="LblToDoTime" runat="server" Text='<%# Bind("ToDoTime",
    > "{0:hh:mm tt}") %>'></asp:Label>


    You have to use a template field for DateTime formatting. For some
    reason it doesn't work for Bound fields.

    It is in a Template. Below is the section of GridView.

    <asp:TemplateField HeaderText="My To Do"
    SortExpression="ToDo">
    <EditItemTemplate>
    </EditItemTemplate>
    <ItemTemplate>
    <table>
    <tr>
    <td colspan="3">
    <asp:LinkButton ID="LBtnToDo"
    runat="server" Text='<%# Bind("ToDo") %>' OnClientClick="opentasks(0);return
    false;"></asp:LinkButton>
    </td>
    </tr>
    <tr>
    <td>By:</td>
    <td><asp:Label ID="LblToDoDate"
    runat="server" Text='<%# Bind("ToDoDate", "{0:M/d/yy}")
    %>'></asp:Label></td>
    <td><asp:Label ID="LblToDoTime"
    runat="server" Text='<%# Bind("ToDoTime", "{0:hh:mm tt}")
    %>'></asp:Label></td>
    </tr>
    </table>

    </ItemTemplate>
    </asp:TemplateField>
    David C, Aug 21, 2009
    #3
  4. David C

    David C Guest

    "Stan" <> wrote in message
    news:...
    On 20 Aug, 19:57, "David C" <> wrote:
    > I have a Label bound to a Time(0) column data type and want it to display
    > hh:mm tt (e.g. 09:15 AM) when the Time column contains 09:00:00 I have the
    > Label as below but it displays 09:00:00 and wondered if my format was
    > wrong.
    > I have used this on a datetime column before and it worked great. Thanks.
    >
    > David
    >
    > <asp:Label ID="LblToDoTime" runat="server" Text='<%# Bind("ToDoTime",
    > "{0:hh:mm tt}") %>'></asp:Label>


    You have to use a template field for DateTime formatting. For some
    reason it doesn't work for Bound fields.

    Also, it worked when ToDoTime was a DateTime column on SQL Server but now it
    is a Time(0) field on SQL Server 2008. Hope this helps.

    David
    David C, Aug 21, 2009
    #4
  5. David C

    David C Guest

    "Mark Rae [MVP]" <> wrote in message
    news:...
    > "David C" <> wrote in message
    > news:...
    >
    >>I have a Label bound to a Time(0) column data type and want it to display
    >>hh:mm tt (e.g. 09:15 AM) when the Time column contains 09:00:00 I have
    >>the Label as below but it displays 09:00:00 and wondered if my format was
    >>wrong. I have used this on a datetime column before and it worked great.
    >>Thanks.
    >>
    >> David
    >>
    >> <asp:Label ID="LblToDoTime" runat="server" Text='<%# Bind("ToDoTime",
    >> "{0:hh:mm tt}") %>'></asp:Label>

    >
    > You need to add HtmlEncode="false" e.g.
    >
    > <asp:Label ID="LblToDoTime" runat="server" Text='<%# Bind("ToDoTime",
    > "{0:hh:mm tt}") %>' HtmlEncode="false" />
    >
    > Ignore the reply about TemplateFields...
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net


    Mark,
    That didn't work either. I changed the page code to below but 2:00 PM still
    displays as 14:00:00

    David

    <td><asp:Label ID="LblToDoTime" runat="server" Text='<%# Bind("ToDoTime",
    "{0:hh:mm tt}") %>' HtmlEncode="False"></asp:Label></td>
    David C, Aug 21, 2009
    #5
  6. David C

    David C Guest

    "Mark Rae [MVP]" <> wrote in message
    news:eyqt%...
    > "David C" <> wrote in message
    > news:...
    >
    >> That didn't work either. I changed the page code to below but 2:00 PM
    >> still displays as 14:00:00

    >
    >
    > We've been here before, haven't we...?
    > http://www.microsoft.com/communitie...f3-ae1c-8d04d7482fd9&cat=&lang=&cr=&sloc=&p=1
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net


    Yes, that is why I am confused. The only difference is that the one I am
    currently having trouble with is in a Master Page and is in a TemplateField
    with a table. See full GridView below.

    <asp:GridView ID="gvTasks" runat="server"
    AutoGenerateColumns="False"
    BackColor="White" BorderColor="#999999" BorderStyle="Solid"
    BorderWidth="1px"
    CellPadding="3" DataKeyNames="TaskID"
    DataSourceID="SqlTasks" ForeColor="Black"
    GridLines="Vertical" EmptyDataText="No Current Tasks"
    AllowPaging="True"
    PageSize="7" Width="100%">
    <EmptyDataRowStyle BackColor="Blue" ForeColor="White"
    Width="150px" HorizontalAlign="Center" />
    <Columns>
    <asp:BoundField DataField="TaskID" HeaderText="TaskID"
    ReadOnly="True"
    SortExpression="TaskID" Visible="False" />
    <asp:TemplateField HeaderText="My To Do"
    SortExpression="ToDo">
    <EditItemTemplate>
    </EditItemTemplate>
    <ItemTemplate>
    <table>
    <tr>
    <td colspan="3">
    <asp:LinkButton ID="LBtnToDo"
    runat="server" Text='<%# Bind("ToDo") %>' OnClientClick="opentasks(0);return
    false;"></asp:LinkButton>
    <asp:TextBox ID="txtPersonID"
    runat="server" Text='<%# Bind("PersonID") %>' CssClass="Hide"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>By:</td>
    <td><asp:Label ID="LblToDoDate"
    runat="server" Text='<%# Bind("ToDoDate", "{0:M/d/yy}")
    %>'></asp:Label></td>
    <td><asp:Label ID="LblToDoTime"
    runat="server" Text='<%# Bind("ToDoTime", "{0:hh:mm tt}") %>'
    HtmlEncode="false"></asp:Label></td>
    </tr>
    </table>

    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <FooterStyle BackColor="#CCCCCC" />
    <PagerStyle BackColor="#999999" ForeColor="Black"
    HorizontalAlign="Center" />
    <SelectedRowStyle BackColor="#000099" Font-Bold="True"
    ForeColor="White" />
    <HeaderStyle BackColor="Black" Font-Bold="True"
    ForeColor="White"
    Font-Size="Medium" />
    <AlternatingRowStyle BackColor="#CCCCCC" />
    </asp:GridView>
    David C, Aug 22, 2009
    #6
  7. David C

    pallavi_a

    Joined:
    Oct 28, 2012
    Messages:
    1
    Hi ,

    I am facing a similar problem. I have a textbox bound to a Time(0) column data type in sql server 2008 and want it to display
    hh:mm tt (e.g. 09:00 AM) when the Time column contains 09:00:00

    <asp:TextBox ID="TimeInTextBox_Item" runat="server" Text='<%# Bind("TimeIn","{0:hh\:mm tt}") %>' CssClass="textboxFont" Width="70px" >
    </asp:TextBox>

    But it throws error when the above format is given. I also tried with hh:mm tt but it still throws error.

    Let me know if someone can help me with that. Thanks
    pallavi_a, Oct 28, 2012
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shaiboy_UK
    Replies:
    0
    Views:
    472
    Shaiboy_UK
    Oct 19, 2003
  2. flamesrock
    Replies:
    8
    Views:
    438
    Hendrik van Rooyen
    Nov 24, 2006
  3. Summu82
    Replies:
    5
    Views:
    853
    Richard Heathfield
    Jun 7, 2006
  4. dch3
    Replies:
    2
    Views:
    506
  5. ruca

    Format textbox to time format

    ruca, May 6, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    830
    HJohns
    May 9, 2005
Loading...

Share This Page