如何在日期和时间中允许空值 [英] How to allow the null value in the Date And time
本文介绍了如何在日期和时间中允许空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好我在数据网格视图中导入excel 1表。在具有日期和时间格式的记录的excel文件中,如果任何记录单元格为空如何保存记录,而我保存记录则显示错误字符串未被识别为有效日期时间
Hi all I import the excel 1 sheet in data grid view. In the excel file having Records in date and time format, if any cell of record is null how to save the record, while I am save the record it’s showing the Error "String was not recognized as a valid Date Time"
推荐答案
如下所示初始化您的DateTime属性
Initialize Your DateTime property like below
public DateTime? Date{get;set;}
希望这有帮助
Hope this helps
最简单的选择是使用可空类型 [ ^ ]。
Eg的DateTime? dt = null;
这里 [ ^ ]就是一个很好的例子。
但是,您需要确保在显示之前将此值检查为null。
The easiest option would be to use a nullable type[^].
E.g.DateTime? dt = null;
Here[^] is a good example.
However, you will need to make sure you check this value for null before display.
C#支持可以约会的日期时间 [ ^ ]
C# does support Nullable DateTime[^]
DateTime? MyNullableDate = null;
是一个有效的声明。
is a valid statement.
这篇关于如何在日期和时间中允许空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文