Data Grid Date Formating

  • Thread starter Thread starter JAPHET
  • Start date Start date
J

JAPHET

I am Trying to Format date in my Datagrid to SHORTDATE format with the
following codes,

Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
DataGrid1.ItemDataBound

Dim dt As DateTime

dt = e.Item.Cells(2).Text

e.Item.Cells(2).Text = dt.ToString("dd-mm-yyyy")

End Sub

but I get the error Message

Cast from string "Entry Date" to type 'Date' is not valid.
 
Hi Japhet:

What type of column are you using? It would probably be easier to
format the column by setting a property, for instance the BoundCoumn
has a DataFormatString property you can use to tell the grid how to
format values.

If you use ItemDataBound you have to check what type of item is
binding. From the error message it appears you might be trying to
format the column header (the string "Entry Date") as a date by first
converting the string to a date.

First check e.Item.ItemType to make sure it is a
ListItemType.AlternatingItem or a ListItemType.Item before trying to
format.

HTH,
 
Back
Top