将DateTime转换为ShortDateString时出错? [英] Error while converting DateTime to ShortDateString?

查看:118
本文介绍了将DateTime转换为ShortDateString时出错?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用这一行将DateTime转换为ShortDateString。



I can convert the DateTime to ShortDateString using this line.

li.SubItems.Add(CType(dt.Rows(i).Item("student_dob"), DateTime).ToShortDateString)





但下一行是错误的。为什么?请提供解决方案。





But this next line is giving an error. Why? Please provide a solution.

li.SubItems.Add(CType(("student_dob"), DateTime).ToShortDateString)





错误是:从字符串student_dob到日期类型的转换无效。



The error is: Conversion from string "student_dob" to type 'Date' is not valid.

推荐答案

你得到那个错误,因为你试图将字符串student_dob字面转换为DateTime。这是不可能的,因为它根本不是有效日期。首先必须获取要转换的项目,就像在第一个代码块中一样。
You get that error because you are trying to convert the string "student_dob" literally to a DateTime. This is not possible because it is not a valid date at all. You first have to fetch the item to convert, like you did in your first code block.


让我们试着看看发生了什么:

该行试图将项目添加到可能是ListView的项目:

Let's try to see what is happening:
The line tries to add an item to something that may be a ListView:
li.SubItems.Add( ... )



添加到视图的内容是:


What is added to the view is:

CType( "..Value to cast..", "..Destination Type..").ToShortDateString()



这里要施放的值是字符串 student_dob,目的地类型是 System.DateTime

您要求计算机打开文字字符串student_dob进入DateTime。因此错误。


Here the value to cast is the string "student_dob" and the destination type is System.DateTime.
You are asking the computer to turn the literal string "student_dob" into a DateTime. Thus the error.


这篇关于将DateTime转换为ShortDateString时出错?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆