我希望在DateTimePicker上设置VS2015中的最小日期和最大日期。我希望将最小日期设置为数据库中的值,将最大日期设置为DateTime.Now。我有以下代码:
SQLiteCommand cmdForShopRegDate = new SQLiteCommand(@"select Date from [ShopRegistration]", con);
SQLiteDataAdapter AdapterShopRegDate = new SQLiteDataAdapter(cmdForShopRegDate);
DataTable TableShopRegDate = new DataTable();
AdapterShopRegDate.Fill(TableShopRegDate);
this.dateTimePickerStartReport.MaxDate = System.DateTime.Now.Date;
this.dateTimePickerStartReport.MinDate = Convert.ToDateTime(TableShopRegDate.Rows[0][0].ToString());我得到了以下错误:
“18-7月-28 12:00 AM”的值对“MinDate”无效。'MinDate‘必须小于MaxDate。
发布于 2018-08-02 18:04:42
你的问题不是很详细,你似乎是在要求别人为你做你的工作。尽管这可能是一次学习经验,请阅读如何问一个彻底的问题。
您所遇到的问题与数据的格式有关。您正在解析18-Jul-28的值。问题是,这个解析为7/18/2028,这肯定比8/2/2018大。要解决这个问题,您需要使用以下格式进行解析:
yy-MMM-dd除此之外,您的代码也可以简化(除非您绝对需要DataTable)。SqlLiteCommand.ExecuteScalar返回结果集中第一行的第一列,并忽略所有其他数据。
using (SqlLiteConnection conn = new SqlLiteConnection("put your connection string here")) {
using (SqlLiteCommand cmd = new SqlLiteCommand("select Date from [ShopRegistration]", conn) {
conn.Open();
dateTimePicker.MinDate = DateTime.ParseExact((string)cmd.ExecuteScalar(),
"yy-MMM-dd",
CultureInfo.InvariantCulture).Date;
}
}
dateTimePicker.MaxDate = DateTime.Now.Date;您需要将using System.Globalization;添加到访问CultureInfo的用法中。
参考资料
https://stackoverflow.com/questions/51659530
复制相似问题