进入 DataGrid 我正在显示月份中的所有“工作”日(列DateColumn):
DataGrid
ColX | DateColumn | ColZ 1 | 2018-09-03 | A 10 | 2018-09-04 | AA 8 | 2018-09-05 | A1 234 | 2018-09-06 | C20
现在我正在尝试将日期列表更改为下一个月和上一个月的构建方法。我的问题是从我的 ObservableCollection
ObservableCollection
private void PrevMonth_Executed(object obj) { //Console.WriteLine(MyConceptItems.ElementAt(0).DateColumn);--2018-09-03 int month = DateTime.ParseExact(MyConceptItems.ElementAt(0). DateColumn, "YYYY-MM-DD", CultureInfo.InvariantCulture).Month }
下面是我的错误:
Exception thrown: 'System.FormatException' in mscorlib.dll An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll String was not recognized as a valid DateTime.
DateColumn DateTime 你可以这样做:
DateColumn
DateTime
int month = MyConceptItems.ElementAt(0).DateColumn.Month;
如果它是一个 string 你真的应该把类型改成 日期时间 . 或者在解析 一串
string
日期时间
一串
int month = DateTime.ParseExact(MyConceptItems.ElementAt(0).DateColumn, "yyyy-MM-dd", CultureInfo.InvariantCulture).Month;