Home > Enterprise >  new row in datagrid with custom data
new row in datagrid with custom data

Time:09-30

I have two datagrids. The first one is an overview about a time tracking. The second one shows some details.

For example: The first datagrid contains each day, where a employee works. The second datagrid contains the time stamping for a day.

However, when I add a new stamping row, the controls in the datagrid are empty. That is not really a problem but the datepicker shows the date 01/01/0001.

what i want is when i add a new row, the date field should be filled with the date from the first datagrid.

VIEW:

<DataGrid Grid.Column="0"
                      IsReadOnly="True"
                      AutoGenerateColumns="False"
                      Grid.Row="0"
                      x:Name="DgStundenView"
                      Margin="0 0 10 0"
                      SelectedItem="{Binding SelectedItem}"
                      ItemsSource="{Binding TimeOverviewList}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Wochentag" Binding="{Binding Wochentag}" />
                <DataGridTextColumn Header="Datum" Binding="{Binding Tag}" />
                <DataGridTextColumn Header="Arbeitszeit Soll" Binding="{Binding ArbeitszeitInStunden}" />
                <DataGridTextColumn Header="Arbeitszeit gesamt" Binding="{Binding GesamtDauerInStunden}" />
                <DataGridTextColumn Header="Pausenzeit" Binding="{Binding Pausenzeit}" />
                <DataGridTextColumn Header="Pausendifferenz" Binding="{Binding PausenDifferenzInStunden}" />
                <DataGridTextColumn Header="Arbeitszeit inkl. Pause" Binding="{Binding TatsaechlicheDauerInStunden}" />
                <DataGridCheckBoxColumn Header="Status" Binding="{Binding StempelungVorhanden,Mode=OneWay}" />
                <DataGridTextColumn Header="Info" Binding="{Binding Info}" />
            </DataGrid.Columns>

            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Wochentag}" Value="Sa">
                            <Setter Property="Background" Value="LightGray" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Wochentag}" Value="So">
                            <Setter Property="Background" Value="LightGray" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>

        <DataGrid Grid.Column="0"
                  Grid.Row="1"
                      x:Name="DgStempelungen"
                      Margin="0 10 10 0"
                      AutoGenerateColumns="False"
                  CanUserAddRows="True"
                  SelectedItem="{Binding SelectedValue}"
                  ItemsSource="{Binding TimeDetailList}">


            <DataGrid.Resources>
                <x:Array x:Key="Reasons" Type="system:String">
                    <system:String>NICHT ANWENDBAR</system:String>
                    <system:String>KRANK</system:String>
                    <system:String>GANZER TAG URLAUB</system:String>
                </x:Array>
            </DataGrid.Resources>

            <DataGrid.Columns>

                <DataGridTextColumn Header="ID" IsReadOnly="True" Binding="{Binding  Id}"/>
                <DataGridTemplateColumn Header="KOMMEN DATUM">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <DatePicker SelectedDate="{Binding  ComeBooking}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="KOMMEN UHRZEIT">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <materialDesign:TimePicker Text="{Binding ComeBookingTime, StringFormat=t}"
                                                       Is24Hours="True">
                            </materialDesign:TimePicker>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="GEHEN DATUM">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <DatePicker SelectedDate="{Binding GoBooking}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="GEHEN UHRZEIT">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <materialDesign:TimePicker Text="{Binding GoBookingTime, StringFormat=t}"
                                                       Is24Hours="True">
                            </materialDesign:TimePicker>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridComboBoxColumn Header="GRUND"
                                        ItemsSource="{StaticResource Reasons}"
                                        TextBinding="{Binding Info}"/>
            </DataGrid.Columns>
</DataGrid>

VIEW MODEL

[CanBeNull] private ClassTimeTrackingDayStamp _selectedItem;

[CanBeNull]
public ClassTimeTrackingDayStamp SelectedItem
{
    get { return _selectedItem; }
    set
    {
        _selectedItem = value;
        OnPropertyChanged();
        if (_selectedItem != null)
            TimeDetailList = new ObservableCollection<ClassTimeTrackingTimeStamp>(_selectedItem.Stempelungen);
    }
}

private ObservableCollection<ClassTimeTrackingDayStamp> _timeOverviewList;

public ObservableCollection<ClassTimeTrackingDayStamp> TimeOverviewList
{
    get { return _timeOverviewList; }
    set
    {
        _timeOverviewList = value;
        OnPropertyChanged();
    }
}

[CanBeNull] private ObservableCollection<ClassTimeTrackingTimeStamp> _timeDetailList;

[CanBeNull]
public ObservableCollection<ClassTimeTrackingTimeStamp> TimeDetailList
{
    get { return _timeDetailList; }
    set
    {
        _timeDetailList = value;
        OnPropertyChanged();
    }
}

MODEL

public class ClassTimeTrackingTimeStamp : ViewModelBase
{
    public DateTime ComeBooking { get; set; }

    public DateTime GoBooking { get; set; }

    public TimeSpan ComeBookingTime { get; set; }
    public TimeSpan GoBookingTime { get; set; }

    public int Id { get; set; }

    public string Info { get; set; }
}

Update

EVENT OVER MVVM VIEW

<DataGrid Grid.Column="0"
                  Grid.Row="1"
                      x:Name="DgStempelungen"
                      Margin="0 10 10 0"
                      AutoGenerateColumns="False"
                  CanUserAddRows="True"
                  SelectedItem="{Binding SelectedValue}"
                  ItemsSource="{Binding TimeDetailList}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="AddingNewItem">
                    <i:InvokeCommandAction Command="{Binding Path=AddNewItemCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
             ...

VIEW MODEL

public ICommand AddNewItemCommand { get; set; }

#endregion Properties

#region Events

public event EventHandler AddNewItemEventHandler;

#endregion

#region Constructor

public WTimeClockOverviewVM()
{
    AddNewItemCommand = new RelayCommand(o =>
    {
        this.AddNewItemEventHandler?.Invoke(this, EventArgs.Empty);
    });
}

CODE BEHIND

public WTimeClockOverview()
{
    InitializeComponent();

    var vm = (WTimeClockOverviewVM)DataContext;

    if(vm == null) return;
    vm.AddNewItemEventHandler  = (sender, args) =>
    {
        var selectedItemInFirstDg = vm.SelectedItem;

        if (selectedItemInFirstDg != null)
        {
            var newItem = new ClassTimeTrackingTimeStamp();
            newItem.ComeBooking = selectedItemInFirstDg.Tag;
        }
    };
}

Thank you for your help :)

Best regards Levin

CodePudding user response:

In second DataGrid, you can subscribe to AddingNewItem event

<DataGrid x:Name="DgStempelungen" 
          AddingNewItem="DgStempelungen_OnAddingNewItem"
          ...

In code behind

private void DgStempelungen_OnAddingNewItem(object sender, AddingNewItemEventArgs e)
{
    // I'll assume that the Model's Name in first DataGrid collection is StudentItem!
    var selectedItemInFirstDg = DgStundenView.SelectedItem as StudentItem;
    // or you can get the selected item from the DataContext:
    // (DgStundenView.DataContext as MyViewModel)?.SelectedItem;
    if (selectedItemInFirstDg != null){
       // create the new row with data from selectedItemInFirstDg, ex. date, etc...
       e.NewItem = new ClassTimeTrackingDayStamp
       {
            ComeBooking = selectedItemInFirstDg.Date; // for example
       };
    }
}
  • Related