lapucca
asked on
How to format and retrieve a Datatime data from a gridview control?
<asp:BoundField DataField="AccountExpirati onDate" HeaderText="Expiration Date" HeaderStyle-VerticalAlign= "Middle" HeaderStyle-HorizontalAlig n="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.AccountExpiration Date = Convert.ToDateTime(editRow .Cells[5]. Controls[0 ]);
I'm also attaching a couple of code snipets. Thank you
grid.txt
rowUpdating.txt
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.AccountExpiration
I'm also attaching a couple of code snipets. Thank you
grid.txt
rowUpdating.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.Parse Exact(Stri ng s, String format, DateTimeFormatInfo dtfi, DateTimeStyles style)
at System.DateTime.ParseExact (String s, String format, IFormatProvider provider)
at NursesTempAccounts._Defaul t.gridAcco unts_RowUp dating(Obj ect sender, GridViewUpdateEventArgs e) in c:\Projects\NursesTempAcco unts\Nurse sTempAccou nts\Defaul t.aspx.cs: line 177
at System.Web.UI.WebControls. GridView.O nRowUpdati ng(GridVie wUpdateEve ntArgs e)
at System.Web.UI.WebControls. GridView.H andleUpdat e(GridView Row row, Int32 rowIndex, Boolean causesValidation)
at System.Web.UI.WebControls. GridView.H andleEvent (EventArgs e, Boolean causesValidation, String validationGroup)
at System.Web.UI.WebControls. GridView.O nBubbleEve nt(Object source, EventArgs e)
at System.Web.UI.Control.Rais eBubbleEve nt(Object source, EventArgs args)
at System.Web.UI.WebControls. GridViewRo w.OnBubble Event(Obje ct source, EventArgs e)
at System.Web.UI.Control.Rais eBubbleEve nt(Object source, EventArgs args)
at System.Web.UI.WebControls. LinkButton .OnCommand (CommandEv entArgs e)
at System.Web.UI.WebControls. LinkButton .RaisePost BackEvent( String eventArgument)
at System.Web.UI.WebControls. LinkButton .System.We b.UI.IPost BackEventH andler.Rai sePostBack Event(Stri ng eventArgument)
at System.Web.UI.Page.RaisePo stBackEven t(IPostBac kEventHand ler sourceControl, String eventArgument)
at System.Web.UI.Page.RaisePo stBackEven t(NameValu eCollectio n postData)
at System.Web.UI.Page.Process RequestMai n(Boolean includeStagesBeforeAsyncPo int, Boolean includeStagesAfterAsyncPoi nt)
InnerException:
System.FormatException was unhandled by user code
HResult=-2146233033
Message=String was not recognized as a valid DateTime.
Source=mscorlib
StackTrace:
at System.DateTimeParse.Parse
at System.DateTime.ParseExact
at NursesTempAccounts._Defaul
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.Control.Rais
at System.Web.UI.WebControls.
at System.Web.UI.Control.Rais
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.Page.RaisePo
at System.Web.UI.Page.RaisePo
at System.Web.UI.Page.Process
InnerException:
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].ToStri ng()"?
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}
Could you post the string returned by "editRow.Cells[5].Controls
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}
ASKER
editRow.Cells[5].Controls[ 0].ToStrin g() 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(((TextB ox)(editRo w.Cells[5] .Controls[ 0])).Text) ;
Thank you.
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
This is how I got to work
Convert.ToDateTime(((TextB
Thank you.
ASKER
dateError.png