Solved

format datetime as date in edititemtemplate of gridview

Posted on 2013-06-06
9
1,123 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

724 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