0

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

3
Contributors
4
Replies
22
Views
3 Years
Discussion Span
Last Post by FenrirMX
1

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.

0

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 by taekiewzz

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.