从列中获取当前月份到 ObservableCollection
Get current month from column into ObservableCollection
进入 DataGrid
我正在显示一个月中的所有 "working" 天(DateColumn 列):
ColX | DateColumn | ColZ
1 | 2018-09-03 | A
10 | 2018-09-04 | AA
8 | 2018-09-05 | A1
234 | 2018-09-06 | C20
现在我正在尝试将日期列表更改为下个月和上个月的构建方法。我的问题是从我的 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
你可以这样做:
int month = MyConceptItems.ElementAt(0).DateColumn.Month;
如果它是 string
,您真的应该将类型更改为 DateTime
。或者在解析 string
值时使用以下格式:
int month = DateTime.ParseExact(MyConceptItems.ElementAt(0).DateColumn, "yyyy-MM-dd", CultureInfo.InvariantCulture).Month;
进入 DataGrid
我正在显示一个月中的所有 "working" 天(DateColumn 列):
ColX | DateColumn | ColZ
1 | 2018-09-03 | A
10 | 2018-09-04 | AA
8 | 2018-09-05 | A1
234 | 2018-09-06 | C20
现在我正在尝试将日期列表更改为下个月和上个月的构建方法。我的问题是从我的 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
你可以这样做:
int month = MyConceptItems.ElementAt(0).DateColumn.Month;
如果它是 string
,您真的应该将类型更改为 DateTime
。或者在解析 string
值时使用以下格式:
int month = DateTime.ParseExact(MyConceptItems.ElementAt(0).DateColumn, "yyyy-MM-dd", CultureInfo.InvariantCulture).Month;