Time format in GridView

  • Thread starter Thread starter David C
  • Start date Start date
D

David C

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>
 
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.
 
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>
 
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
 
Mark Rae said:
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,
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>
 
Mark Rae said:

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>
 
Back
Top