formatting a date inside a datacolumn - please urgent

Hi There,

How can I format a date inside a data column? I have been looking around but can't find any information on it.

Sean - thank in advance

c.Controls.Add(New LiteralControl(dataR(dataC.ColumnName)))
LVL 5
bullroutAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

surajgupthaCommented:
Try giving

{D} or {d} in the formatting column
0
surajgupthaCommented:
{D} or {d} should be in the Data Formatting expression. It can be added for a bound column in the property builder. which in HTML looks like
<asp:BoundColumn DataFormatString="{D}"></asp:BoundColumn>

Otherwise it can be done in the Item Data Bound event of the datagrid



0
bullroutAuthor Commented:
Hi There,

What would the syntax be?

c.Controls.Add(New LiteralControl(dataR(dataC.ColumnName)))
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

mmarinovCommented:
Hi bullrout,

have you tried
DateTime.ToString("d")
see here
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemdatetimeclasstostringtopic1.asp
Regards!
B..M
mmarinov
0
surajgupthaCommented:
Hi Bullrout,
             I dont know how to add a DataColumn at runtime as you have done.

Check if there is an attribute called DataFormatString for DataC.

Regards,
Suraj
0
raed_hasanCommented:
Hi
 In grid properties on your column you there is a field Named
DataFormatString and you can make your format as follow :
{0:dd/MM/yyyy HH:mm}
be carefull  mm = minutes and MM = month
0
raed_hasanCommented:
Oops surajguptha  I didnt notice that u mentioned DataFormatString in ur reply , sorry.

Regards
0
ryerrasCommented:
If I assume dataC.ColumnName returns the date, then

c.Controls.Add(New LiteralControl(dataR(dataC.ColumnName.ToString("d")))
0
bullroutAuthor Commented:
Hi There,

I tried the code above "c.Controls.Add(New LiteralControl(dataR(dataC.ColumnName.ToString("d")))" with no luck. The database table is a datetime datatype, do I need to explcitly declare the column inside the datatable as this datatype too?

Sean

 dt.Columns.Add("wipdue")
0
ryerrasCommented:
sorry I guess I put the parenthesis in the wrong place. try with the following one more time
c.Controls.Add(New LiteralControl(dataR(dataC.ColumnName)).ToString("d")));
0
bullroutAuthor Commented:
Hi There,

I get the error, public overridable function() as string has no parameters and it's reutrn type cannot be indexed.

Help!
0
ryerrasCommented:
Hmm how about this
c.Controls.Add(New LiteralControl(CType(DateTime, dataR(dataC.ColumnName)).ToString("d")));
0
bullroutAuthor Commented:
still no joy, it complains about not using the datetime as an expression, and the ctype clause make the program not recognise the datacoloumn.
0
mmarinovCommented:
i think ryerras makes a little mistake
try this

c.Controls.Add(New LiteralControl(CType(dataR(dataC.ColumnName), DateTime).ToString("d")));

B..M
0
Volkan VardarSoftware Team LeaderCommented:
c.Controls.Add(New LiteralControl(Format(Cdate(dataR(dataC.ColumnName)), "dd.MM.yyyy")))

and take a look at this article
http://www.dotnetforce.com/Content.aspx?t=a&n=205
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bullroutAuthor Commented:
Hi All,

The code changes seemed to have got rid of the error but the format of the date does not change.

if I use this c.Controls.Add(New LiteralControl(CType(dataR(dataC.ColumnName), DateTime).ToString("d")));

I still get this:

03/07/2004 12:00:00 AM

if I use this c.Controls.Add(New LiteralControl(Format(Cdate(dataR(dataC.ColumnName)), "dd.MM.yyyy")))

I still get this:

03/07/2004 12:00:00 AM

Any idea why the formatting is not taking effect?

0
ryerrasCommented:
what do you want the date format should be
0
bullroutAuthor Commented:
Hi there,

I just want the date format to be (03/07/2004).
0
ryerrasCommented:
ok then use this

c.Controls.Add(New LiteralControl(CType(dataR(dataC.ColumnName), DateTime).ToString("MM:dd:yyyy")));
0
bullroutAuthor Commented:
I have tried this and with not luck, it does not make a difference to the format at all.
0
ryerrasCommented:
do one thing, try with different formats like ToString("C") , ToString("F") and ToString("R") , see if the result is the same. Then there must be something wrong somewhere else, not when you are adding the control. Let us know if you are getting the same result
0
bullroutAuthor Commented:
sorry ryerras, my mistake :)

It's working well now, I appreciate the help.

Sean
0
ryerrasCommented:
cool. thats great to hear
0
mmarinovCommented:
also you can use

CType(dataR(dataC.ColumnName), DateTime).ToShortDateString()

B..M
mmarinov
0
KrummelzCommented:
What if you are creating the data table like so:

DataTable finalStatement = new DataTable();
finalStatement.Columns.Add(new DataColumn("Date", typeof(DateTime)));

how would you format the column now?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.