Query between dates with a single datetimepicker form C#

David Cortes 1 Reputation point
2022-05-17T23:44:03.113+00:00

I would like to consult two dates from a gridview but in a single datetimpicker, I know what the mechanism is to do it with two but I want to do it with one and I have seen programs that have it.

Developer technologies Windows Forms
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2022-05-18T04:11:53.007+00:00

    @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:

    203052-4.gif

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.