Data Grid Date Formating

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.
 
S

Scott Allen

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,
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top