Search code examples
c#wpfxamldesktop-applicationdesktop

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?


Solution

  • 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); 
                };
    
            }