Solved

How to format and retrieve a Datatime data from a gridview control?

Posted on 2014-12-21
5
293 Views
Last Modified: 2014-12-21
<asp:BoundField DataField="AccountExpirationDate" HeaderText="Expiration Date" HeaderStyle-VerticalAlign="Middle" HeaderStyle-HorizontalAlign="Center" />
In this column, what can I use for DataFormat string so it displays a short date from the binded datatime data, I don't want the time to display.

Also, how can I retrieve from this column when user update the data.  Currently my code below gives me error.
editUser.AccountExpirationDate = Convert.ToDateTime(editRow.Cells[5].Controls[0]);

I'm also attaching a couple of code snipets.  Thank you
grid.txt
rowUpdating.txt
0
Comment
Question by:lapucca
[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
  • 3
  • 2
5 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 500 total points
ID: 40512100
To display short date, you can use Dataformatstring property as follows:
<asp:BoundField DataField="AccountExpirationDate" HeaderText="Expiration Date" HeaderStyle-VerticalAlign="Middle" HeaderStyle-HorizontalAlign="Center" DataFormatString="{0:d}" />

Open in new window


To convert the string you need to specify the DT format as shown here :
editUser.AccountExpirationDate = DateTime.ParseExact(editRow.Cells[5].Controls[0].ToString(), "MM/dd/yyyy", null);

Open in new window

Assumption: Your date/time is being entered in US format("MM/dd/yyyy"), change it accordingly based on your server culture. You may consider changing {0:d} to the same format of your mapping to avoid the server culture changing the format the day is represented.
0
 

Author Comment

by:lapucca
ID: 40512335
Hi, the DataFormatString property worked great.  However, I got error, attached screen shot.   Thank you.
dateError.png
0
 

Author Comment

by:lapucca
ID: 40512338
Here is the error detail.

System.FormatException was unhandled by user code
  HResult=-2146233033
  Message=String was not recognized as a valid DateTime.
  Source=mscorlib
  StackTrace:
       at System.DateTimeParse.ParseExact(String s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
       at System.DateTime.ParseExact(String s, String format, IFormatProvider provider)
       at NursesTempAccounts._Default.gridAccounts_RowUpdating(Object sender, GridViewUpdateEventArgs e) in c:\Projects\NursesTempAccounts\NursesTempAccounts\Default.aspx.cs:line 177
       at System.Web.UI.WebControls.GridView.OnRowUpdating(GridViewUpdateEventArgs e)
       at System.Web.UI.WebControls.GridView.HandleUpdate(GridViewRow row, Int32 rowIndex, Boolean causesValidation)
       at System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup)
       at System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e)
       at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
       at System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e)
       at System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
       at System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)
       at System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 40512339
The error means that the date time string is not being written with the defined format("MM/dd/yyyy").
Could you post the string returned by "editRow.Cells[5].Controls[0].ToString()"?
I need to check in which format the date time string is returned.
Also need to know what is the server culture. (e.g. en-US ,en-UK ,etc.); as stated in the assumption notes on my previous post  {0:d} returns server short date which may be different from MM/dd/yyyy, if this the case then  you will need to change {0:d} to {0:MM/dd/yyyy}
0
 

Author Comment

by:lapucca
ID: 40512353
editRow.Cells[5].Controls[0].ToString() returns this  "System.Web.UI.WebControls.TextBox"

I put a watch on this expression and it returns the date.  So it would need to first access the Text property to get the data value of the date.
            ((TextBox)(editRow.Cells[5].Controls[0])).Text      " 12/21/2014 12:00:00 AM"      string

This is how I got to work
Convert.ToDateTime(((TextBox)(editRow.Cells[5].Controls[0])).Text);

Thank you.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

739 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