none
2 DTP able filter DGV? RRS feed

  • Question

  • Hi good morning,

    I would like to ask if its possible to filter data(w/o SQL) in Dgv using 2 datetimepicker with 2 separate dates? I have been searching online but cant find an example

    Thank you

    Tuesday, June 18, 2019 1:53 AM

Answers

  • Hi Mendel,

    Here is a demo that filter the data that between two dates. The code is shown as follows.

        public Form1()
        {
            InitializeComponent();
            pickerFrom.CustomFormat = "yyyy-MM-dd";
            pickerFrom.Format = DateTimePickerFormat.Custom;
            pickerTo.CustomFormat = "yyyy-MM-dd";
            pickerTo.Format = DateTimePickerFormat.Custom;
            dataGridView1.AllowUserToAddRows = false;
        }
    
        private void btFilter_Click(object sender, EventArgs e)
        {
            DateTime dateFrom = DateTime.Parse(pickerFrom.Text);
            DateTime dateTo = DateTime.Parse(pickerTo.Text);
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                DataGridViewRow row = dataGridView1.Rows[i];
                DateTime current = (DateTime)row.Cells[1].Value;
                Console.WriteLine(current.ToShortDateString());
                if (!(current >= dateFrom && current <= dateTo))
                {
                    dataGridView1.Rows.Remove(row);
                    i--;
                }
            }
        }

    Result,

    Hope this can help you.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 18, 2019 3:12 AM
    Moderator

All replies

  • Hi Mendel,

    Here is a demo that filter the data that between two dates. The code is shown as follows.

        public Form1()
        {
            InitializeComponent();
            pickerFrom.CustomFormat = "yyyy-MM-dd";
            pickerFrom.Format = DateTimePickerFormat.Custom;
            pickerTo.CustomFormat = "yyyy-MM-dd";
            pickerTo.Format = DateTimePickerFormat.Custom;
            dataGridView1.AllowUserToAddRows = false;
        }
    
        private void btFilter_Click(object sender, EventArgs e)
        {
            DateTime dateFrom = DateTime.Parse(pickerFrom.Text);
            DateTime dateTo = DateTime.Parse(pickerTo.Text);
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                DataGridViewRow row = dataGridView1.Rows[i];
                DateTime current = (DateTime)row.Cells[1].Value;
                Console.WriteLine(current.ToShortDateString());
                if (!(current >= dateFrom && current <= dateTo))
                {
                    dataGridView1.Rows.Remove(row);
                    i--;
                }
            }
        }

    Result,

    Hope this can help you.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 18, 2019 3:12 AM
    Moderator
  • Thank you!

    Tuesday, June 18, 2019 9:31 AM