I want to filter my datagrid view in my winform with a date range.
I have 2 label with StartingDate(lblStart) and EndingDate(lblEnd) which is called from the datepicker on another form.

Filter = "(DATE >= '" + Convert.ToDateTime(lblStart.Text).ToString("dd/MM/yyyy") + "' and DATE <= '" + Convert.ToDateTime(lblEnd.Text).ToString("dd/MM/yyyy") + "')";

And this code does not works. Can someone tell me that is it any wrong on my code, or any changes need to be done on datagridview in order make the filter works.
My DATE column data is in the format of dd/MM/yyyy which loaded from my SQL db.
Thanks

The problem is that you're using a ' to qualify your datetime.
While that's the correct way of doing it for SQL, this isn't SQL, you need to use #

Filter = "(DATE >= #" + Convert.ToDateTime(lblStart.Text).ToString("dd/MM/yyyy") + "# and DATE <= #" + Convert.ToDateTime(lblEnd.Text).ToString("dd/MM/yyyy") + "#)";

Like so.

Hi FenrirMX, if I am not mistaken the format for the DateTime should be in "MM/dd/yyyy"
Anyway, your code works fine when I using the format of "MM/dd/yyyy"...thanks

Edited 3 Years Ago by taekiewzz

This question has already been answered. Start a new discussion instead.