问题描述
dateTimePicker1.Value = dss.Tables["ITdata"].Rows[0]["pudate"].ToString();
推荐答案
dateTimePicker1.Value = dss.Tables["ITdata"].Rows[0]["pudate"].ToString();
如果已经是一个DateTime,则只需取消ToString-将日期强制转换为字符串是一个不好的主意,除非您要显示它们,否则以后可能会产生本地化问题.
If it is already a DateTime, then just take the ToString off - it is a bad idea to start casting dates to strings unless you are about to display them, and it can create localisation problems later.
dateTimePicker1.Value = dss.Tables["ITdata"].Rows[0]["pudate"];
如果它是一个DateTime,但作为通用object
返回,则将其强制转换:
If it is a DateTime, but it is returned as a generic object
then cast it:
dateTimePicker1.Value = (DateTime) dss.Tables["ITdata"].Rows[0]["pudate"];
如果它是字符串,则需要解析该字符串,并希望该字符串格式与您的本地系统相同! :laugh:如果是特定的字符串格式,则可以使用DateTime.ParseExact或DateTime.TryParseExact.
If it is a string, then you need to Parse the string, and hope like heck that the string format is the same as your local system! :laugh: If it is a specific string format, then DateTime.ParseExact or DateTime.TryParseExact will be the way to go.
DateTime val;
if (DateTime.TryParse(dss.Tables["ITdata"].Rows[0]["pudate"].ToString(), out val))
{
dateTimePicker1.Value = val;
}
else
{
/**
assign some fallback value to dateTimePicker1.Value, or raise an exception, or
do whatever needs to be done.
**/
}
dateTimePicker1.Value = Convert.ToDateTime(dss.Tables["ITdata"].Rows[0]["pudate"].ToString());
这篇关于错误:无法将类型'string'隐式转换为'System.DateTime'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!