Solved

format datetime as date in edititemtemplate of gridview

Posted on 2013-06-06
9
1,070 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

14 Experts available now in Live!

Get 1:1 Help Now