Reputation: 1536
I have column called DateColumn which stores creation date like this:
created at 2017-01-20
created at 2017-01-21
created at 2017-01-22
created at 2017-01-23
created at 2017-01-24
How can I get only the dates between 20 and 24 while the column also contains "created at" string?
I tried this and it worked but only on columns with just the date in theme no words:
(dataGridView1.DataSource as DataTable).DefaultView.RowFilter = "DateColumn >= '" + minDate + "' AND DateColumn <= '" + maxDate + "'";
Upvotes: 1
Views: 1423
Reputation: 3979
I think your problem is that "created at" is inside your column. Now you have to find workaround for this. You better should consider if it's necessary that "created at" is there. If you select these values (inside your DataTable) from a Database you could substring this in your select.
For example (Oracle-Style):
SELECT someValue1, someValue2, TO_DATE(SUBSTR(DateColumn, 10, 10), 'YYYY-MM-DD') DateColumn
FROM something
If you definitive need this text what probably isn't the case you should make a new column into your DataTable which only consists of the date. You can filter this column then:
DataTable yourTable = new DataTable();
//Fill with strange strings like 'created at 2017-01-01'
yourTable.Columns.Add("RealDate");
foreach (DataRow row in yourTable.Rows)
{
//Instead of substr you can also use regex if you prefer
row["RealDate"] = Convert.ToDateTime(Substring(row["DateColumn"], 10, 10));
}
After both of my solutions you can easily filter the new row for DateTime as you mentioned in your question.
Upvotes: 1