发生无效的强制转换异常

发生无效的强制转换异常

问题描述:

我想在列表视图中显示数据

我的代码是

DtMfgDate.Value = VaccineLedgerView.SelectedItems(0).SubItems(2).Text

I want to show data in listview

my code is

DtMfgDate.Value = VaccineLedgerView.SelectedItems(0).SubItems(2).Text

看起来像DtMfgDate是某种日期控件,并且可能期望有效日期分配给它的值.

试试这个:
Looks like DtMfgDate is some sort of date control and would probably expect a valid date value assigned to it.

Try this:
DtMfgDate.Value = Date.Parse(VaccineLedgerView.SelectedItems(0).SubItems(2).Text)



您需要执行一些额外的编码,以确保解析的值实际上是有效日期.您也许可以考虑使用TryParse函数.

http://msdn.microsoft.com/en-us/library/497a406b.aspx [ ^ ]

祝你好运!



You need to do some extra coding to make sure that the value parsed is actually a valid date. You maybe could look into the TryParse function for that.

http://msdn.microsoft.com/en-us/library/497a406b.aspx[^]

Good luck!


尝试..

try..

DtMfgDate.Value = Convert.ToDateTime(VaccineLedgerView.SelectedItems(0).SubItems(2).Text).ToString("dd-MMM-yyyy");