dataformatstring template column why is getting a short date so ha

C

cindy

I have datagrid, datasource datafield is datetime so its
ugly date time etc ugly I want short date
in datagrid column is datetime datatype so it sorts right
not like text so thats good but its ugly

have tried this but the error is invalid cast
<asp:TemplateColumn SortExpression="reviseddate" HeaderText="Revised Date">
<ItemTemplate><asp:Label runat="server" Text='<%#
((DateTime)DataBinder.Eval(Container.DataItem,
"reviseddate")).ToShortDateString() %>' ID="Label2"></asp:Label>
</ItemTemplate>

have tried this but then the grid just does not display it binds and then
cannot display

the code below was auto created, as a bound column the use of
dataformatstring
works great then using property builder in design mode and convert to
template
column you get code below but then run and no grid display

<ItemTemplate><asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
"DataItem.reviseddate", "{0:d}") %>'></asp:Label></ItemTemplate>
 
S

Steven Cheng[MSFT]

Hi cindy,

Thank you for posting.

Regarding the data formatting in DataGrid column for DateTime data field, I
think it is likely the data binding expression in the first code snippet
you pasted hasn't been evaluated correctly by the runtime. I think you can
try call the GetType method on the returned object and print it out to see
whether it is of the correct type like DataBinder.Eval(Container.DataItem,
"reviseddate").GetType().ToString(). Also, the casting error may also
occur on other step in the databinding. You can define a helper function in
the page's codebehind class to do the formatting. e.g:

<%# MyConvertFunc( DataBinder.Eval(......)) %>

in code behind, define:

protected string MyConvertFunc(object obj)
{
.................
}

Please feel free to let me know if there is anything else I missed.

Hope this helps.

Regards,

Steven Cheng
Microsoft Online Community Support


==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.



Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top