@David Cortes , you could try to set two proprties and use dateTimePicker1_ValueChanged event to query data between two dates in a single datetimepicker.
Here is a code example you could refer to.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public DateTime formatter { get; set; }
public DateTime latter { get; set; }
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
formatter = latter;
latter = dateTimePicker1.Value;
}
private void button1_Click(object sender, EventArgs e)
{
DateTime replace;
if (formatter > latter)
{
replace = latter;
latter = formatter;
formatter = replace;
}
string query = string.Format("Date>= '{0}' and Date<='{1}'", formatter, latter);
var result = dt.Select(query).AsEnumerable().CopyToDataTable();
textBox1.Text=String.Format("You choosed from {0} to {1}",formatter,latter);
dataGridView1.DataSource = result;
}
DataTable dt = new DataTable();
private void Form1_Load(object sender, EventArgs e)
{
dateTimePicker1.ShowUpDown = true;
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Date", typeof(DateTime));
dt.Rows.Add(1001, "test1", DateTime.Parse("2022-05-01"));
dt.Rows.Add(1002, "test2", DateTime.Parse("2022-06-01"));
dt.Rows.Add(1003, "test3", DateTime.Parse("2022-07-01"));
dataGridView1.DataSource = dt;
}
}
Tested Result:
Note: Here I want to explain why I use the UpDown style for the datetimepicker. If I use the normal style of the datetimepicker and choose the date cross the month, the formatter value will be always show the first day of the latter month.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.