[英]C# FTP Response.LastModified always returns 1/1/0001 12:00:00 AM
[英]leave datetime picker returns 1 / 1 / 0001 12:00:00 AM in c#
我在課堂上定義
public DateTime? LineCheckSubmitDateTime { set; get; }
我需要使用gridview
初始化此變量
但是有時我需要將此值保留為null,但是當我將其保留時,它將返回1 / 1 / 0001 12:00:00 AM
所以這是我的代碼:
newObj.LineCheckSubmitDateTime = (Convert.ToDateTime(gridViewDetails.GetRowCellValue(rowHandle, "LineCheckSubmitDateTime"))==DateTime.Parse("1 / 1 / 0001 12:00:00 AM") ) ? Convert.ToDateTime(gridViewDetails.GetRowCellValue(rowHandle, "LineCheckSubmitDateTime")):null;
有兩個問題:
1:此代碼返回此錯誤:
Severity Code Description Project File Line
Error CS0173 Type of conditional expression cannot be determined because there is no implicit conversion between 'DateTime' and '<null>'
2:您有更好的解決方案嗎?
該錯誤是因為您必須將條件運算符的至少一個操作數顯式轉換為DateTime?
更好的方法是將其與DateTime.MinValue
進行比較,而不是將最小日期字符串轉換為DateTime
,並且還緩存轉換后的值,然后在條件運算符中使用它,而不是對其進行兩次轉換。
var tempDateConverted = Convert.ToDateTime(gridViewDetails.GetRowCellValue(rowHandle, "LineCheckSubmitDateTime"));
newObj.LineCheckSubmitDateTime = tempDateConverted == DateTime.MinValue ?
null : (DateTime?) tempDateConverted;
您還可以將null
顯式轉換為DateTime?
在上述聲明中。
我不確定GridView
的LineCheckSubmitDateTime
控件,這很可能是它的值已經是一個DateTime
對象,並在object
內部返回。 您也可以嘗試:
object obj = gridViewDetails.GetRowCellValue(rowHandle, "LineCheckSubmitDateTime");
newObj.LineCheckSubmitDateTime = (DateTime?) obj;
使用上面的代碼,您不必調用Convert.ToDateTime
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.