Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Date is changed in DataGrid when selected entity is passed to another window and bound to DataGrid

$
0
0

Hello, everyone.

I have the following situation. There's a DataGrid in main window with flights bound to it. I have button which sends selected flight into helper window to edit this flight. Here's the initial code:

1. DataGrid in main window with flights:

<DataGrid Grid.Row="1" x:Name="dataGrid" AutoGenerateColumns="False"><DataGrid.Columns><DataGridTextColumn Header="Flight Number" IsReadOnly="True" Binding="{Binding FlightNumber}" /><DataGridTextColumn Header="Payment Date By Agent To Executor" IsReadOnly="True" Binding="{Binding PaymentDateByAgentToExecutor, StringFormat=\{0:dd.MM.yyyy\}}" /><DataGridTextColumn Header="Flight Date" IsReadOnly="True" Binding="{Binding FlightDate, StringFormat=\{0:dd.MM.yyyy\}}" /></DataGrid.Columns></DataGrid>

2. The code that sends selected flight to helper window to edit it:

private void EditFlight()
{
    var selectedFlight = dataGrid.SelectedItem as Flight;
    if (selectedFlight == null) return;
    var flightWindow = new EditFlight(selectedFlight);
    flightWindow.ShowDialog();
}


3. The code in helper window which receives flight to edit:

public partial class EditFlight : Window
{

    private Flight flight = null;
    private FlightsContext db = null;

    public EditFlight(Flight FlightToEdit)
    {
        InitializeComponent();

        db = new FlightsContext();
        flight = db.Flights.Add(FlightToEdit);
        db.Entry(flight).State = EntityState.Unchanged;
        this.DataContext = flight;
        this.Title = $"Edit Flight - {flight.FlightNumber}";
    }

}

4. XAML in helper window:

<Grid><DatePicker HorizontalAlignment="Left" Margin="10,127,0,0" VerticalAlignment="Top" Width="120" Text="{Binding PaymentDateByAgentToExecutor, Mode=TwoWay, StringFormat=\{0:dd.MM.yyyy\}}"/></Grid>

I have list of flights in main DataGrid. For one flight the date for PaymentDateByAgentToExecutor is 10.08.2016 (August, 10). But as soon as this flight is bound to the helper window, the value is changed to 08.10.2016 (October, 9) whereas helper window shows correct result - 10.08.2016. Also, when I choose some other date (in helper window) in DatePicker, the value of date become incorrect (swapped month and day) - and again also incorrect in main DataGrid. Tests showed that this is due to TwoWay mode. How to fix it? :)



There is no knowledge that is not power.





Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>