简体   繁体   中英

Dataview custom filter on time component of datetime column

I have datagrid view with 2 columns.

| ID | DateCol             |
| 5  | 2021-02-12 03:05:01 |
| 4  | 2021-02-15 02:05:01 |
| 3  | 2021-02-15 05:05:01 |
| 2  | 2021-02-13 05:05:01 |
| 1  | 2021-02-12 05:05:01 |

On applying custom filter to collection view:

BindingListCollectionView collectionView = CollectionViewSource.GetDefaultView(ItemsSource);
collectionview.CustomFilter = $"CONVERT(DateCol, 'System.String') >= #05:00:00#";

I get only one result:

   | 3  | 2021-02-15 05:05:01 |

Seems like it's taking DateTime of today that is 15th Feb. How to filter just based on time?

Expected output is all 3 items which has time of day component greater than 0500.

    | 3  | 2021-02-15 05:05:01 |
    | 2  | 2021-02-13 05:05:01 |
    | 1  | 2021-02-12 05:05:01 |

Thanks

this.defaultView.Filter =
            w =>
            {
                var dateTimeColumn = w as DataTimeColumn;
                return dateTimeColumn?.DateTimeCol.Hour > 5;
            };

        this.defaultView.Refresh();

you can achieve the filtering with the above piece of code when you are using ICollectionView as ItemsSource for your DataGrid.

Try this:

BindingListCollectionView collectionView = CollectionViewSource.GetDefaultView(ItemsSource) as BindingListCollectionView;
collectionView.CustomFilter = $"SUBSTRING(CONVERT(DateCol, 'System.String'), 11, 8) >= #05:00:00#";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM