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

Recommended Answers

All 4 Replies

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.

Please post your complete code ,

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

No prob, I had trouble the first time I wanted to filter by date too.
Cheers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.