Solved

format datetime as date in edititemtemplate of gridview

Posted on 2013-06-06
9
1,100 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

860 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