David Tunnell
David Tunnell

Reputation: 7542

Formatting MM/dd in gridview date row

I am trying to format a date on a grid view to only show the month and the day:

<asp:BoundField DataField="Monday" HeaderText="Monday" ReadOnly="True" SortExpression="Monday" ItemStyle-HorizontalAlign="Right"
     DataFormatString="{0:MM/dd}" HtmlEncode="false" />

It is no working. Also, when I look here:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx

I don't see this formatting as an option. How do I achieve this?

Edit:

enter image description here

Upvotes: 0

Views: 245

Answers (2)

Humpy
Humpy

Reputation: 2012

I would change that column into a TemplateField. Then I would change the Text to..

Text='<%# DataBinder.Eval(Container.DataItem, "Monday","{0:MM/dd}") %>'

This should work for you. The entire TemplateField for and just the ItemTemplate should look something similar to this..

 <asp:TemplateField HeaderText="Monday" SortExpression="Monday">
                    <ItemTemplate>
                        <asp:Label ID="lblMonday" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Monday","{0:MM/dd}") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

I hope this helps!

Upvotes: 1

Black Frog
Black Frog

Reputation: 11713

Is your Data Field Monday a DateTime field in your result.

If it isn't a datetime field, you will need to convert it your Sql statement.

For example:

Select <expression> as Monday....

to

Select Cast(<expression> as datetime) as Monday....

Upvotes: 0

Related Questions