Need syntax help formatting a dateTime from a databinder.eval

D

darrel

I'm binding fileinformation to a repeater and attempting to display the
'lastWriteTime' property as a formatted date.

This:

<%# DataBinder.Eval(Container.DataItem, "LastWriteTime" %>

Gives me the full date/time.

This:

<%# DataBinder.Eval(Container.DataItem, "{MM/DD/YYYY}" %>

Gives me a ' Overload resolution failed because no accessible 'ToString' can
be called without a narrowing conversion: '

Aha! It's not returning a dateTime value, but rather a string. Short of
parsing this string with regex or something, is there a way to get it to
return an actual dateTime value so I can use the format string on it?

-Darrel
 
E

Eliyahu Goldin

Darrel,

You should convert it ItemDataBound event in normal way, no regex is needed.

A c# example (assuming you are formatting column #2):

private void myGrid_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
ListItemType itemType = e.Item.ItemType;
if ((itemType == ListItemType.Pager) ||
(itemType == ListItemType.Header) ||
(itemType == ListItemType.Footer))
{
return;
}
e.Item.Cells[2].Text=System.Convert.ToDateTime
(e.Item.Cells[2].Text).ToString(formatExpression);
}

Eliyahu
 
E

Eliyahu Goldin

Matt,

As Darrel said, the column value comes as a string. Your reference is good
for DateTime values, not for strings.

Eliyahu
 
D

darrel

Eliyahu and Matt:

Thank you both. Actually, I ended up getting it to work by casting from
string to date back to string with the format:

<%# ctype(DataBinder.Eval(Container.DataItem, "LastWriteTime"),
date).tostring("d") %>

It seems redundant to me, but seems to work!

-Darrel
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top