Time format in GridView

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>
 
S

Stan

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.
 
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>

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

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
 
D

David C

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>
 
D

David C

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>
 
Joined
Oct 28, 2012
Messages
1
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top