Reputation: 147
The problem I'm having is that the filter will not accept the time part of a datetime.
For clarity,
This works.
" AND NextWorkDate <= #" + DateTime.Now.AddDays(1).ToString("dd.M.yyyy") + "#"
This doesnt work.
" AND NextWorkDate <= #" + DateTime.Now.AddDays(1).ToString("dd.M.yyyy H:mm:ss") + "#"
Whenever I try to include the time aspect of the datetime, the filter just doesnt work, it returns no rows.
I've tried it a thousand different ways and I really cant figure out why it won't take the damn time.
The datasource column that the filter is comparing to is a datetime with the format dd.M.yyyy H:mm:ss.
The database column is formatted completely differently, as yyyy-mm-dd hh:mm:ss:fff
Could this be causing some problem along the line? I think it isn't because the filter is applied to the datasource, not the database.
Upvotes: 2
Views: 5463
Reputation: 147
Solved.
The problem was my own foolishness. There was another class filtering the string before it went to the bindingsource.
Upvotes: 0
Reputation: 460228
There is a lack of documentation
how to filter a BindingSource
(or DataTable, DataView etc) by time instead of by Date only.
But this format should work:
"AND NextWorkDate <= #yyyy-MM-dd HH:mm:ss#"
You can get this format by DateTime.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture)
.
string filter = string.Format("...AND NextWorkDate <= #{0}#",
DateTime.Now.AddDays(1).ToString(
"yyyy-MM-dd HH:mm:ss",
CultureInfo.InvariantCulture));
However, i would prefer Linq since it is more readable, maintainable and more powerful. So you can filter the underlying list with linq. If you use a DataTable
use Linq-To-DataSet
, otherwise use Linq-To-Sql
or Linq-To-Entities
to query the datasource or Linq-To-Objects
if it's just an in-memory list. Then you just have to reassign the DataSource
.
Upvotes: 1