Date Formating

S

Seli

Hello

I would like to format a date in the DataGrid to be 3.3.2004 and not with the time (default?) 3.3.2004 00:00:00!

<asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 80px; POSITION: absolute; TOP: 264px
runat="server"> AutoGenerateColumns="True" </asp:DataGrid

DataGrid1.DataSource=ds.Tables["Products"].DefaultView
DataGrid1.DataBind()

DataGrid1.Columns[0].DataFormatString = "{0:d}"

The last line does not work

Thank
Seli
 
S

Scott Mitchell [MVP]

Seli said:
I would like to format a date in the DataGrid to be 3.3.2004 and not with the time (default?) 3.3.2004 00:00:00!!

<asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 80px; POSITION: absolute; TOP: 264px"
runat="server"> AutoGenerateColumns="True" </asp:DataGrid>

DataGrid1.DataSource=ds.Tables["Products"].DefaultView;
DataGrid1.DataBind();

DataGrid1.Columns[0].DataFormatString = "{0:d}";

Two things:

1.) You need to set the DataFormatString *before* the call to DataBind()
2.) The Columns[0] returns a DataGridColumn class instance. This class
doesn't have a DataFormatString. The BoundColumn class does. So you
need to cast Columns[0] to a BoundColumn before trying to muddle with
the DataFormatString property. Something like:

((BoundColumn) DataGrid1.Columns[0]).DataFormatString = "{0:d}";

Remember to place this **before** the DataBind() call...

Happy Programming!

--

Scott Mitchell
(e-mail address removed)
http://www.4GuysFromRolla.com
http://www.ASPFAQs.com
http://www.ASPMessageboard.com

* When you think ASP, think 4GuysFromRolla.com!
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top