How do I format data in a datalist?

  • Thread starter Thread starter jm
  • Start date Start date
J

jm

I have a datalist and am trying to format the data.

I have tried:

DataFormatString="{0:MM-dd-yyyy}"

but it does not work (works fine if it is a datagrid and a
boundcolumn):

<asp:Label id="order_date" Text='<%#
DataBinder.Eval(Container.DataItem,"order_date") %>'
DataFormatString="{0:MM-dd-yyyy}" runat="server" />

Using this:

Convert.ToDateTime(DataBinder.Eval(Container.DataItem,"order_date")).ToString("MMM,
dd yyy")%

works.

However, if the data is null (no value from the database); I get an
error:

System.InvalidCastException: Object cannot be cast from DBNull to
other types

How do I format the data, even if it is null?

Also, can I do inline coding here? Tried it to no avail.

One last, I am thinking my only way to do this may be the
OnItemDataBound event. Is this correct?

Thank you for any help.
 
Back
Top