I have a gridview, grid has 5 different columns ID,FirstName,LastName,DateOfBirth and Age, gridview can be edited, so I want to update the Age depending on DateOfBirth column so I have written the necessary functionality in OnRowBound function.When i execute the grid view page I get “String is not Recognized as valid datetime”.
Here is the Function of OnRowBound
protected void UserInfoGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DateTime DOBOnGrid = DateTime.ParseExact(e.Row.Cells[3].Text,"MM/dd/yyyy",null);
Label tAge = (Label)e.Row.Cells[4].FindControl("txtAge");
TimeSpan ts = DateTime.Now - DOBOnGrid;
int CurrAge = ts.Days / 365;
tAge.Text = CurrAge.ToString();
}
}
<asp:GridView ID="UserInfoGrid" runat="server" AutoGenerateColumns="False" CellPadding="3"
DataKeyNames="userid" DataSourceID="DataSrcUserInfo"
AllowPaging="True" OnRowDataBound="UserInfoGrid_RowDataBound"
OnRowUpdated="Update" OnRowDeleted="Delete" PageSize="10" >
can anyone help me with this
Thanks,
First of all, shouldn’t the code your are showing be in the
RowUpdatingevent handler? As for the error you are getting, its obviously due to a bad formatted string in the Birthday cell.Have you checked for empty strings? I’m guessing you are getting the exception even before the
GridViewrenders on screen as its in theRowDataBoundevent handler which fires when the GridView is loading.