• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1800
  • Last Modified:

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)))
0
bullrout
Asked:
bullrout
  • 8
  • 7
  • 3
  • +4
3 Solutions
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 8
  • 7
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now