2011-05-27 67 views
1

我有一个gridview,网格有5个不同的列ID,名字,姓氏,DateOfBirth和年龄,gridview可以编辑,所以我想更新Age取决于DateOfBirth列我已经在OnRowBound函数中编写了必要的功能。当我执行网格视图页面时,我得到“字符串不被识别为有效的日期时间”。不能更新列取决于GridView中的其他列

这里是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" > 

谁能帮我这个

感谢,

回答

1

首先,不应该是你显示的代码是在RowUpdating事件处理程序?至于你得到的错误,显然是由于生日单元格中格式不正确的字符串造成的。

您是否检查过空字符串?我猜你甚至在GridView在屏幕上呈现它的异常,在RowDataBound事件处理程序中它会在GridView加载时触发。