DateTimePicker不显示所选日期

时间:2016-10-19 06:54:14

标签: c# sql-server sql-server-2008 c#-4.0

 var dtp = dateTimePicker1.Value.Date;
 var dtp2 = dateTimePicker2.Value.Date ;

SqlDataAdapter da1 = new SqlDataAdapter("SELECT * FROM Tabledbstatus WHERE Date BETWEEN'" + dateTimePicker1.Value.ToString() + "' and '" + dateTimePicker2.Value.ToString() + "'", conn);

da1.Fill(ds1, "TS1"); 

dv1 = new DataView(ds1.Tables["TS1"]);

dataGridView1.DataSource = ds1.Tables["TS1"];

示例:我选择日期01-09-2016至03-09-2016不显示日期01-09-2016但显示02-09-2016至03-09-2016

*我希望展示日期01-09-2016至03-09-2016 *

2 个答案:

答案 0 :(得分:0)

您可以更改以下代码行:

SqlDataAdapter da1 = new SqlDataAdapter("SELECT * FROM Tabledbstatus WHERE Date BETWEEN'" + dateTimePicker1.Value.ToString() + "' and '" + dateTimePicker2.Value.ToString() + "'", conn);

SqlDataAdapter da1 = new SqlDataAdapter("SELECT * FROM Tabledbstatus WHERE Date BETWEEN'" + dateTimePicker1.Value.Date.ToString() + "' and '" + dateTimePicker2.Value.Date.AddDays(1).ToString() + "'", conn);

如果为dateTimePicker1选择01-09-2016,为dateTimePicker2选择03-09-2016,它将获得01-09-2016 00.00.00 AM到04-09-2016 00.00.00 AM之间的值

答案 1 :(得分:0)

它可能会导致日期时间字段,您应尝试选择从中添加小时的日期。

SqlDataAdapter da1 = new SqlDataAdapter("SELECT * FROM Tabledbstatus WHERE Date BETWEEN'" + dateTimePicker1.Value.ToString() + " 00:00:00' and '" + dateTimePicker2.Value.ToString() + " 23:59:59'", conn);

修改 因为它在第一次出现问题时你可以尝试这样的事情:

// after line
var dtp = dateTimePicker1.Value.Date;
// add 
dtp = dtp.AddDays(-1.0);