简体   繁体   中英

DateTimePicker don't show selected date

 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"];

Example : I select date 01-09-2016 to 03-09-2016 don't show date 01-09-2016 but show 02-09-2016 to 03-09-2016

* I want show date 01-09-2016 to 03-09-2016 *

You can change this line of code:

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

to

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

It will get the value between 01-09-2016 00.00.00 AM to 04-09-2016 00.00.00 AM if you select 01-09-2016 for dateTimePicker1 and 03-09-2016 for dateTimePicker2

It may cause datetime field, you shall try to select date adding hours from to.

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

Edit: As it makes the problem for you at first date, you could try something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM