Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to filter datagridview between two datetimepicker.
I'm using c# and MySQL .

What I have tried:

private void ShowButton (object sender, EventArgs e)
      {
          String constring ="datasource=;port=;username;password=";
          using (var conn = new MySqlConnection(constring))
          {

                      BindingSource bs = new BindingSource();

                      bs.DataSource = dataGridView1.DataSource;

                      bs.Filter = "Convert(Date,'System.String') Between  '%" + dateTimePicker1.Text + "%' AND '%" + dateTimePicker2.Text + "%'  ";
            dataGridView1.DataSource = bs;
          }

     }
C#

Posted
Updated 28-Dec-21 3:05am
v3
Comments
CHill60 7-Apr-17 7:00am    
Why not filter it before you populate the dataGridView? What query are you using to populate the DGV?
EM_Y 7-Apr-17 7:40am    
I updated it you can see it now !
I hope it's more clear !
it's a button show
CHill60 7-Apr-17 7:47am    
I asked "What query are you using to populate the DGV". This button is using the existing DataSource for the DGV. How was that datasource populated?
Use the  Reply  button when replying so posters are notified of your response
EM_Y 10-Apr-17 7:05am    
I'm sorry for my miss understand I used Datagridview Tasks to choose data Source !!and it populate its data automatically.

Your filter needs to conform to the syntax described in the remarks of the DataColumn.Expression property:
Expression property | Microsoft Docs[^]

Try:
C#
bs.Filter = string.Format("Date Between #{0:yyyy/MM/dd}# And #{1:yyyy/MM/dd}#", dateTimePicker1.Value, dateTimePicker2.Value);
 
Share this answer
 
Comments
EM_Y 10-Apr-17 3:36am    
Thank you but I tried your code and it showed me this Error Message 'The expression contains unsupported operator 'Between'. !
Richard Deeming 10-Apr-17 13:23pm    
Well, according to the documentation, it should be supported.

Try using >= and <= instead:
bs.Filter = string.Format("Date >= #{0:yyyy/MM/dd}# And Date <= #{1:yyyy/MM/dd}#", dateTimePicker1.Value, dateTimePicker2.Value);
EM_Y 12-Apr-17 5:21am    
I tried it , and the message error change to " Column 'Date' not known " !
Richard Deeming 12-Apr-17 14:12pm    
So change the filter to specify the correct column name!
bs.Filter = string.Format("YOUR_COLUMN_NAME_HERE >= #{0:yyyy/MM/dd}# And YOUR_COLUMN_NAME_HERE <= #{1:yyyy/MM/dd}#", dateTimePicker1.Value, dateTimePicker2.Value);
EM_Y 13-Apr-17 6:46am    
It's working I'm very thankful .
thanks a lot
your help me as well
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900