WPF DatePicker Datagrid Filter

I want to filter the data in my datagrid by date. The start and end date will be entered and the data between those dates will be returned.

The data type in which I keep the date in my database is DateTime;

2024-09-09 19:32:50.160

like this.

This is my datagrid. I’m working on a parking lot project. The left one DatePicker is startDate and the other one is endDate

My XAML Code;

<Grid Width="300" HorizontalAlignment="Left" Grid.Row="4" Margin="110 0 15 0" >
    <DatePicker Name="startDatePicker" SelectedDateChanged="StartDatePicker_SelectedDateChanged" Width="140" Margin="20,1,140,0" VerticalAlignment="Center" />

    <DatePicker Name="endDatePicker" SelectedDateChanged="EndDatePicker_SelectedDateChanged" Width="140" Margin="165,1,-5,0" VerticalAlignment="Center" />
</Grid>
<DataGrid ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}" Margin="15 20 15 0" RowStyle="{DynamicResource DataGridRowStyle1}"  CellStyle="{DynamicResource DataGridCellStyle1}" x:Name="carActionDataGrid" Style="{DynamicResource DataGridStyle1}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="#" IsReadOnly="True" CanUserResize="False" Width="auto" Binding="{Binding Id}"/>
        <DataGridTextColumn Header="Plaka" Binding="{Binding Plate}" IsReadOnly="True" Width="*" />
        <DataGridTextColumn Header="Giriş Zamanı" Binding="{Binding EntryTime, StringFormat={0:dd.MM.yyyy HH:mm}}" IsReadOnly="True" Width="*"/>
        <DataGridTextColumn Header="Çıkış Zamanı" Binding="{Binding CheckoutTime, StringFormat={0:dd.MM.yyyy HH:mm}}" IsReadOnly="True" Width="*"/>
        <DataGridTextColumn Header="Araç Durumu" Binding="{Binding Status}" IsReadOnly="True" Width="*"/>
        <DataGridTextColumn Header="Kullanıcı" Binding="{Binding Username}" IsReadOnly="True" Width="*"/>
    </DataGrid.Columns>
</DataGrid>

In my XAML Code, I format the data in the data entry before printing it and then print it.

My C# Code;

private void StartDatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
    if (endDatePicker.SelectedDate == null || endDatePicker.SelectedDate < startDatePicker.SelectedDate)
    {
        endDatePicker.SelectedDate = DateTime.Now.Date;
    }

    ApplyDateFilter();
}

private void EndDatePicker_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
    ApplyDateFilter();
}

private void ApplyDateFilter()
{
    if (startDatePicker.SelectedDate == null || endDatePicker.SelectedDate == null)
        return;

    var startDate = startDatePicker.SelectedDate.Value.Date; 
    var endDate = endDatePicker.SelectedDate.Value.Date.AddDays(1).AddTicks(-1); 

    using (var context = new BarcodeReaderEntities())
    {
        var filteredData = context.QRInfoes
            .Where(car =>
                car.EntryTime >= startDate &&
                (car.CheckoutTime == null || car.CheckoutTime <= endDate)  // CheckoutTime might be null
            )
            .ToList();

        carActionDataGrid.ItemsSource = filteredData;
    }
}

I want to filter the data in my datagrid by date. The start and end date will be entered and the data between those dates will be returned. This code is doesn’t work i don’t understand. I wrote a few more codes similar to this, but I don’t think the problem is in the query, maybe it could be in the data type in my database, I couldn’t understand it. Can you guys help me?

3

Your code and explanation are not enough for a precise answer.
For example, there is no indication of what collection, with what type of elements you pass (bind) to carActionDataGrid.ItemsSource when starting the application.
Therefore, the answer will be partially based on guesses.
I will assume that ItemsSource receives a collection with the element type Car.

        private void ApplyDateFilter()
        {
            if (startDatePicker.SelectedDate == null || endDatePicker.SelectedDate == null)
                return;

            DateTime startDate = startDatePicker.SelectedDate.Value.Date;
            DateTime endDate = endDatePicker.SelectedDate.Value.Date.AddDays(1).AddTicks(-1);

            carActionDataGrid.Items.Filter = item =>
            {
                Car car = (Car) item;

                return car.EntryTime >= startDate && (car.CheckoutTime == null || car.CheckoutTime <= endDate); 
            };

        }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật