0

I have created a DataGrid. Now I want to save the cells that I have edited in a database using RestSharp. I read that it works with IEditableObject.

I tried to insert it. Here is the start of my ViewModel:

class LDViewModel : NotifyPropertyBase, IEditableObject

Then the BeginEdit, CancelEdit, and EndEdit methods were created. I added my code to the EndEdit class:

public void EndEdit()
{
    var client = new RestClient("MYDOMAIN");
    var request = new RestRequest("THINGS", Method.POST)
        .AddUrlSegment("name", Name)
        .AddUrlSegment("kompetenzID", LDCurrentKompetenz.KompetenzID)
        .AddUrlSegment("ergebnis", LDCurrentKompetenz.Ergebnis)
        .AddUrlSegment("stufe", LDCurrentKompetenz.Stufe)
        .AddUrlSegment("niveau", LDCurrentKompetenz.Niveau)
        .AddUrlSegment("note", LDCurrentKompetenz.Note)
        .AddUrlSegment("datum", LDCurrentKompetenz.Datum)
        .AddUrlSegment("kuerzel", LDCurrentKompetenz.Kuerzel)
        .AddHeader("Authorization", "Bearer " + ViewFingerPrint);

    IRestResponse restResponse = client.Execute(request);

    if (restResponse.StatusCode == HttpStatusCode.BadRequest)
        MessageBox.Show("Irgendetwas stimmt mit der Verbindung nicht.");
    else
        MessageBox.Show("Die Leistungsdokumentation wurde erfolgreich aktualisiert.");
}

This is what my DataGrid looks like:

<DataGrid ItemsSource="{Binding LDKompetenzen, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" IsReadOnly="False" AutoGenerateColumns="False" CanUserDeleteRows="True" CanUserAddRows="True" CanUserSortColumns="True" FontSize="12" Height="Auto" BorderThickness="0" SelectedItem="{Binding LDCurrentKompetenz.KompetenzID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}">
                    <DataGrid.Resources>
        <Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" TargetType="{x:Type DataGridColumnHeader}" >
            <Setter Property="Background" Value="LightGray" />
            <Setter Property="Foreground" Value="Black" />
            <Setter Property="BorderBrush" Value="Black"/>
            <Setter Property="BorderThickness" Value="1 1 1 1"/>
            <Setter Property="Margin" Value="-1,-1,0,0" />
            <Setter Property="Height" Value="28" />
            <Setter Property="Width" Value="auto"/>
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="FontWeight" Value="SemiBold"/>
            <Setter Property="FontStyle" Value="Italic"/>
            <Setter Property="FontSize" Value="15"/>
            <Setter Property="Padding" Value="15,0,15,0"/>
        </Style>
    </DataGrid.Resources>
    <DataGrid.Columns>
        <DataGridTextColumn Header="Kompetenz" Binding="{Binding Kompetenz, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" IsReadOnly="True">
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="TextBlock.Background" Value="{Binding Background}" />
                    <Setter Property="HorizontalAlignment" Value="Stretch"/>
                    <Setter Property="VerticalAlignment" Value="Stretch"/>
                    <Setter Property="TextAlignment" Value="Center"/>
                    <Setter Property="Padding" Value="0,20,0,0"/>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
        <DataGridTemplateColumn Header="Farben" IsReadOnly="True">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center">
                        <Button Content="" Background="White" Margin="2" Padding="5,3,5,3"></Button>
                        <Button Content="" Background="Yellow" Margin="2" Padding="5,3,5,3"></Button>
                        <Button Content="" Background="LightGreen" Margin="2" Padding="5,3,5,3"></Button>
                    </StackPanel>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Header="Stufe" Binding="{Binding Stufe, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" >
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="HorizontalAlignment" Value="Center"/>
                    <Setter Property="VerticalAlignment" Value="Center"/>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header="Niveau" Binding="{Binding Niveau, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}">
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="HorizontalAlignment" Value="Center"/>
                    <Setter Property="VerticalAlignment" Value="Center"/>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header="Ergebnis" Binding="{Binding Ergebnis, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}">
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="HorizontalAlignment" Value="Center"/>
                    <Setter Property="VerticalAlignment" Value="Center"/>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
        <DataGridTextColumn Header="Note" Binding="{Binding Note, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}">
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="HorizontalAlignment" Value="Center"/>
                    <Setter Property="VerticalAlignment" Value="Center"/>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
        <DataGridTemplateColumn Header="Datum">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <DatePicker SelectedDate="{Binding Datum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}" BorderThickness="0" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Header="Kürzel" Binding="{Binding Kuerzel, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True, NotifyOnTargetUpdated=True}">
            <DataGridTextColumn.ElementStyle>
                <Style TargetType="{x:Type TextBlock}">
                    <Setter Property="HorizontalAlignment" Value="Center"/>
                    <Setter Property="VerticalAlignment" Value="Center"/>
                </Style>
            </DataGridTextColumn.ElementStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

Unfortunately, he does not execute the command with RestSharp. He just doesn't respond.

Where is the mistake? Did I forget something?

Sascha
  • 37
  • 6
  • do you get the messagebox? – Michael Schönbauer Jul 12 '21 at 17:39
  • No, I don't get a message. I don't get any feedback at all. Even a breakpoint at this point does not interrupt the program. – Sascha Jul 12 '21 at 18:22
  • Trying to make this very short. To make this work for MVVM.. EndEdit is for .. kind of.. "apply buttons". Normally, you edit everything and then you hit "apply", thats when your viewmodel has the altered data, and you apply it to the database. add a new column to your datagrid, with a button, call it "apply", let its Command={Binding ApplyCommand} -> make a member Relaycommand called ApplyCommand in your viewmodel, let Applycommand call Endedit. – Michael Schönbauer Jul 12 '21 at 19:08
  • Don't you need a CellTemplate and a *CellEditingTemplate* for your Template columns to trigger edit mode at all and more over to handle the transition back from EditMode to ReadOnlyMode? – lidqy Jul 12 '21 at 19:16
  • It may work in part. Updating and re-entering the database works. However, do I have to fill out all the columns? I want an update to happen if I only edit one column. – Sascha Jul 13 '21 at 11:27

0 Answers0