这是我为了在两个日期之间进行选择而编写的代码(使用DateTimePicker对象)
var query = db.people.AsQueryable(); var fromDate = Convert.ToDateTime(dateTimePicker1); var toDate = Convert.ToDateTime(dateTimePicker2); query = query.Where(x => x.dob > fromDate && x.dob < toDate); clan_savezaBindingSource.DataSource = query.ToList();当我运行代码时,出现错误,说出InvalidCastException 。 我想我应该使用Convert.ToDate以外的东西,但我找不到解决方案。
This is the code that I wrote in order to make a selection between two dates (using DateTimePicker objects)
var query = db.people.AsQueryable(); var fromDate = Convert.ToDateTime(dateTimePicker1); var toDate = Convert.ToDateTime(dateTimePicker2); query = query.Where(x => x.dob > fromDate && x.dob < toDate); clan_savezaBindingSource.DataSource = query.ToList();When I run the code the error appears saying InvalidCastException. I guess I should use something other than Convert.ToDate but I can't find a solution.
最满意答案
您应该使用DateTimePicker Value属性。 它返回选择日期作为DateTime对象:
var fromDate = dateTimePicker1.Value; var toDate = dateTimePicker2.Value;注意: DateTimePicker不是DateTime对象。 并且它不能转换为DateTime 。 DateTimePicker是一个用户控件,它保存DateTime值并提供许多用于呈现自身和处理用户输入的功能。
进一步阅读: 使用DateTimePicker控件
You should use Value property of DateTimePicker. It returns selected the date as DateTime object:
var fromDate = dateTimePicker1.Value; var toDate = dateTimePicker2.Value;Note: DateTimePicker is not a DateTime object. And it's not convertible to DateTime. DateTimePicker is a user control which holds DateTime value and provides a lot of functionality for rendering itself and processing user input.
Further reading: Using DateTimePicker Control
更多推荐
query,Convert,代码,使用,电脑培训,计算机培训,IT培训"/> <meta name="descripti
发布评论