两个日期选择器之间的选择(Selection between two date pickers)

编程入门 行业动态 更新时间:2024-10-27 21:19:40
两个日期选择器之间的选择(Selection between two date pickers)

这是我为了在两个日期之间进行选择而编写的代码(使用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

本文发布于:2023-07-30 07:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336760.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   两个   选择器   pickers   date

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!