我正在開發一個內置于框架 3.5 的 C# Web 應用程式專案,其中 -由于某種奇怪的原因我還沒有弄清楚- 添加了 -(
但是,當系結此值時 -即:GvEmployee.DataBind(); - 在RowDataBound名為“ GvEmployee”的 GridView的“ ”事件中,該值顯示為:
4/03/2013 12:00:00 a. m.- 請注意添加的不間斷空格。
并且System.FormatException發生例外:
System.FormatException: 'String 未被識別為有效的 DateTime。'
此錯誤是由于不間斷空格和a.m.附加字串造成的。如果我洗掉這些字符 - 在運行時除錯時 - ,可以完成 DateTime 轉換。
這是發生錯誤的代碼:
protected void GvEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
{
string id; // This is a reusable variable.
// Check rows only:
if (e.Row.RowIndex >= 0)
{
// Get the value stored in the given cell at the row:
id = e.Row.Cells[3].Text;
// Here, the value of "id" variable is: "4/03/2013 12:00:00 a. m.".
// But, the value returned by the database is: "04/03/2013 12:00:00 a.m.".
// Check if the "id" variable is not empty...
if (!id.Equals(" ") && !string.IsNullOrEmpty(id))
{
// Here fails, due to the value of "id" variable, that is: "4/03/2013 12:00:00 a. m."
// isn't recognized as a valid DateTime value.
e.Row.Cells[3].Text = Convert.ToDateTime(id, CultureInfo.CurrentCulture).ToShortDateString();
}
}
}
如您所見,字串值上的
這很奇怪,因為這個專案已經發布并且只發生在我的筆記本上 - 我相信我用于這個專案的筆記本中缺少一些配置,但是,我還沒有找到任何關于這可能的線索是 - 在這個問題發生在我身上之前,我不知道不間斷空格,我在 Stack Overflow 上找到了這個
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/393738.html
