Avatar of lapucca
lapucca
 asked on

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

<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
C#ASP.NET.NET Programming

Avatar of undefined
Last Comment
lapucca

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Miguel Oz

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
lapucca

ASKER
Hi, the DataFormatString property worked great.  However, I got error, attached screen shot.   Thank you.
dateError.png
lapucca

ASKER
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:
Miguel Oz

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}
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
lapucca

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