简体   繁体   English

如何解决此错误:System.FormatException:'字符串未被识别为有效的DateTime。 从索引0开始有一个未知单词。

[英]how to solve this error: System.FormatException: 'The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.'

Ihave a windows form application C# , when i click on edit on a row, the date in the datagridview have to go into the datetimepicker, but when the day of this date begin's with 0, i obtain this error: 我有一个Windows窗体应用程序C#,当我点击一行上的编辑时,datagridview中的日期必须进入datetimepicker,但是当这个日期的日期开始时为0时,我得到这个错误:

System.FormatException: 'The string was not recognized as a valid DateTime. System.FormatException:'字符串未被识别为有效的DateTime。 There is an unknown word starting at index 0.' 从索引0开始有一个未知单词。 my code is : 我的代码是:

int currentRow = int.Parse(e.RowIndex.ToString());
string ddn = dgv_patients[9, currentRow].Value.ToString();
dateTimePicker_ddn.Text = ddn;

the string ddn looks like 02-May-95, the day begins with 0, and that causes a problem when i try to put ddn in the datetimepicker. 字符串ddn看起来像02-May-95,那天从0开始,当我尝试将ddn放入datetimepicker时会导致问题。

I tried many times to solve this problem, and no solution works! 我多次尝试解决这个问题,没有解决办法!

See the pictures: 看图片:

c# app code and error c#app 代码和错误

You should take a look at the CustomFormat property and verify it fits the Text you are trying to set. 您应该查看CustomFormat属性并验证它是否适合您要设置的Text。

From MSDN - DateTimePicker.Text Property : 来自MSDN - DateTimePicker.Text Property

The string returned by this property is equivalent to the Value property with the appropriate formatting or custom formatting applied. 此属性返回的字符串等效于应用了适当格式或自定义格式的Value属性。 For example, if the Value property is set to 06/01/2001 12:00:00 AM while the CustomFormat property is set to "dddd, MMMM dd, yyyy", the Text property value is "Friday, June 01, 2001". 例如,如果将Value属性设置为06/01/2001 12:00:00 AM而CustomFormat属性设置为“dddd,MMMM dd,yyyy”,则Text属性值为“Friday,June 01,2001” 。
When setting this property, the string must be convertible to an instance of the DateTime class. 设置此属性时,该字符串必须可转换为DateTime类的实例。 It is possible to define a custom format that results in a string that cannot be converted to a valid DateTime value. 可以定义自定义格式,该格式导致无法转换为有效DateTime值的字符串。 Because of this, the string returned from the Text property might cause an error if it is passed back to the Text property. 因此,从Text属性返回的字符串如果传回Text属性可能会导致错误。 If the string cannot be converted to a date/time value, the DateTime class throws a FormatException. 如果字符串无法转换为日期/时间值,则DateTime类将抛出FormatException。

If the column in the DataGrid is already a DateTime then there's no need to use strings, simply assign it to the picker: 如果DataGrid中的列已经是DateTime那么就不需要使用字符串,只需将其分配给选择器即可:

dateTimePicker_ddn.Value = (DateTime)dgv_patients[9, currentRow].Value;

Another option as per the comments is to use DateTime.ParseExact 根据注释的另一个选项是使用DateTime.ParseExact
For example: 例如:

DateTime dt = DateTime.ParseExact("02-May-95", "dd-MMM-yy", CultureInfo.InvariantCulture);

Or in your case exactly: 或者在你的情况下:

dateTimePicker_ddn.Value = DateTime.ParseExact(
    dgv_patients[9, currentRow].Value.ToString(),
    "dd-MMM-yy",
    CultureInfo.InvariantCulture);

The error message is more important that the format of the string itself. 错误消息比字符串本身的格式更重要。

"There is an unknown word starting at index 0" “从索引0开始有一个未知单词”

is not a normal incompatible date format error message. 不是正常的不兼容日期格式错误消息。 Have a look at dnn before it is assigned to the DatePicker , to show what is at index 0 and confirm that you are assigning a string. 在将dnn分配给DatePicker之前,先查看它,以显示索引0处的内容并确认您正在分配字符串。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “System.FormatException:‘该字符串未被识别为有效的 DateTime。有一个从索引 0 开始的未知单词。” 在 C# - "System.FormatException: 'The string was not recognized as a valid DateTime. There is an unknown word starting at index 0." in C# FormatException:该字符串未被识别为有效的DateTime。 从索引0开始有一个未知单词 - FormatException: The string was not recognized as a valid DateTime. There is an unknown word starting at index 0 该字符串未被识别为有效的DateTime。 从索引0开始有一个未知单词。 - The string was not recognized as a valid DateTime. There is an unknown word starting at index 0. label to string 发生异常:-“未将字符串识别为有效的DateTime。 从索引0开始有一个未知词。” - The Exception occured :- “The string was not recognized as a valid DateTime. There is an unknown word starting at index 0.” 该字符串未被识别为有效的DateTime。 从索引26开始有一个未知词 - The string was not recognized as a valid DateTime. There is an unknown word starting at index 26 字符串“未定义”未被识别为有效的日期时间。 有一个从索引“0”开始的未知单词 - The string 'undefined' was not recognized as a valid DateTime. There is an unknown word starting at index '0' 该字符串未被识别为有效的 DateTime。 从索引 0 开始有一个未知单词 - The string was not recognized as a valid DateTime. There is an unknown word starting at index 0 给定System.FormatException:字符串未被识别为有效的DateTime。 在C#中使用datetime.ParseExact - Giving System.FormatException: String was not recognized as a valid DateTime. using datetime.ParseExact in C# System.FormatException: 'String '"2022-04-14 13:03:12"' 未被识别为有效的 DateTime。' - System.FormatException: 'String '"2022-04-14 13:03:12"' was not recognized as a valid DateTime.' 无法识别为有效的日期时间。 从索引0开始的未知单词 - Not Recognized as a valid datetime. Unknown word starting at index 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM