我试着在互联网上效法一些例子,但没有成功。
我正在尝试在GridView1_RowDataBound methode中执行以下操作
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
if (Convert.ToDateTime(drv["Created_Date"]).ToString("dd-MM-yyyy") == "01-01-1900" || (drv["Created_Date"].ToString().Equals(DBNull.Value))) //Check for this date
{
e.Row.Cells[11].Text = "test";
}
}
}我仍然在接收一个对象,它不能从DBNull转换到其他类型。
发布于 2018-03-14 10:09:47
你必须用
string.IsNullOrEmpty(drv["Created_Date"].ToString())但是,您应该切换if-语句中的第一个和第二个值。在转换Created_Date之后,您将检查它是否为null。因此,如果是null,代码仍然会抛出一个错误。所以做这样的事更好
if (string.IsNullOrEmpty(drv["Created_Date"].ToString()) || Convert.ToDateTime(drv["Created_Date"]) == new DateTime(1900, 1, 1))https://stackoverflow.com/questions/49274114
复制相似问题