Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1281
  • Last Modified:

Datagrid date column format

Hi,

I am using VB and ASP.Net and have a stored procedure that returns either a null or a date.  I have tried formatting the date as {0:yyyy-MM-dd h:mm tt} but when there is no date returned (as happens in some rows), it defaults to 1900-01-01....

How do I put conditional logic on this?  I am currently populating the grid as follows:
            DataGrid1.DataSource = sqlcmd.ExecuteReader
            DataGrid1.DataBind()

Appreciate the help.

DP
0
porter416
Asked:
porter416
2 Solutions
 
mmarinovCommented:
Hi porter416,

i suggest you to work in this case with ItemDataBound event
there you can determine if you will output a date or not depending on the value for the current record

here is an example ( it is based on datalist, BUT it is absolutely the same in your case with datagrid - just change
DataListItemEventArgs
to
DataGridItemEventArgs )
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsdatalistclassitemdataboundtopic.asp

Regards!
B..M
mmarinov
0
 
RejojohnyCommented:
try this ... u could use the isnull for the date field in the SQL statement that is used to retrieve the values from the database ..
select isnull(mydatefield, ' ' ) as mydatefield from mytable
this will ensure that an emplty value is returned instead of a null .. just check how the formatting behaves in this case ..


0
 
porter416Author Commented:
Both were great answers and the second 1 was simpler and ultimately the 1 I used.

Thank you both.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now