Solved

format datetime as date in edititemtemplate of gridview

Posted on 2013-06-06
9
1,083 Views
Last Modified: 2013-06-27
Hi

I want to display the date only in a gridview column
The following gives me an error - bind doesnt exist in current context
How can I rectify?
 
                   <asp:Label ID="Labeldts" runat="server" Text='<%# Convert.ToDateTime(Bind("dtStart").ToString("dd/MM/yyyy")) %>'></asp:Label>
0
Comment
Question by:louise_8
  • 5
  • 4
9 Comments
 
LVL 25

Expert Comment

by:Rouchie
ID: 39224854
You should not try to do this.  ASP.NET will automatically format dates based on the user's computer settings, which is important!

You would have to use code-behind to inject the value using a custom date/time pattern if you really want to proceed.  See examples here that create a DateTimeFormatInfo object and format using that: http://msdn.microsoft.com/en-us/library/az4se3k1%28v=vs.71%29.aspx
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 39224881
However, if you're just wanting to remove the time part, you can do this:

                   <asp:Label ID="Labeldts" runat="server" Text='<%# Convert.ToDateTime(Bind("dtStart").ToShortDateString()) %>'></asp:Label>
0
 

Author Comment

by:louise_8
ID: 39239835
Thanks Rouchie

Your 2nd comment is acorrect, I just need to remove the time when the date is displayed

I used your code: Text='<%# Convert.ToDateTime(Bind("dtStart").ToShortDateString()) %>'
However I get the error:       The name 'Bind' does not exist in the current context      

Cheers
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 39240182
Right okay this means you are going to have to use code-behind and push the date into the gridview that way.

Are you using any code-behind already for the gridview?  For example, do you have an OnRowDataBind event in your ASPX.VB/CS file?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 25

Accepted Solution

by:
Rouchie earned 100 total points
ID: 39240189
Anyway here's a full example if you don't already...
In page:
<asp:Label ID="Labeldts" runat="server"></asp:Label> 

Open in new window


In code behind:
Protected Sub myGridViewID_RowDataBound(sender as object, e as GridViewRowEventArgs) Handles myGridViewID.RowDataBound
  Select Case e.Row.RowType
      Case DataControlRowType.DataRow
         Dim Labeldts As Label = CType(e.Row.FindControl("Labeldts"), Label)
         Labeldts.Text = Convert.ToDateTime(e.Row.DataItem("dtStart")).ToShortDateString
  End Select
End Sub

Open in new window

0
 

Author Comment

by:louise_8
ID: 39243192
Thanks
Im almost there, not sure how to handle null values though:

This is the code that works excpet for null values for dtStart:

   DateTime dateToDisplay = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "dtStart"));
            Label Labeldts = e.Row.FindControl("Labeldts") as Label;
            Labeldts.Text = dateToDisplay.ToShortDateString();
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 39243581
That bit again is pretty easy (and applies to all other database columns of different types if you're wanting to format the output!)
Label Labeldts = e.Row.FindControl("Labeldts") as Label;
if (!Information.IsDBNull(e.Row.DataItem("dtStart"))) {
      DateTime dateToDisplay = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "dtStart"));
      Labeldts.Text = dateToDisplay.ToShortDateString(); 
} else {
      Labeldts.Text = "None"
}

Open in new window

0
 

Author Comment

by:louise_8
ID: 39258138
Thanks Rouchie.. getting an error for Information
      The name 'Information' does not exist in the current context      

system namespace is already included..
c#
0
 

Author Comment

by:louise_8
ID: 39258582
actually I've resolved that using
if (! Convert.IsDBNull(DataBinder.Eval(e.Row.DataItem, "dtStart")))

Thanks!

However it seems sometimes I get an error when I click edit:
'Object reference not set to an instance of an object.'
even though its in the if statement
  if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) !=0 ) and shouldnt be executed
the line it points to is
  Labeldts.Text = dateToDisplay.ToShortDateString();
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now