Solved

format datetime as date in edititemtemplate of gridview

Posted on 2013-06-06
9
1,108 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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