0

I am trying to query data (reorders) from my MongoDB database in order to show them in my datagrid. The normal values (key-value pairs) appear perfectly but the array values don't and I don't understand what I am missing. I will post my relevant code below, hoping that someone can tell me what I am missing in order to show the array values inside Bestellung in my datagrid. Thanks in advance for any help, very much appreciated!

Here is the code:

Main Model reorders

public class reorders
{
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; private set; }
    [BsonElement("Hv")]
    public string Hv { get; set; }
    [BsonElement("Bv")]
    public string Bv { get; set; }
    [BsonElement("Bauleiter")]
    public string Bauleiter { get; set; }
    [BsonElement("Empfaenger")]
    public string Empfaenger { get; set; }
    [BsonElement("Empfaenger_Ansprechpartner")]
    public string Empf_Ansprechpartner { get; set; }
    [BsonElement("Empfaenger_Mail")]
    public string Empf_Mail { get; set; }
    [BsonElement("Anlieferungsort")]
    public string Anlieferungsort { get; set; }
    [BsonElement("Anschrift")]
    public string Adressat { get; set; }
    [BsonElement("Adressat")]
    public string Anschrift { get; set; }
    [BsonElement("Plz_Ort")]
    public string Plz_Ort { get; set; }       
    [BsonElement("Kontaktperson_Kontaktnr")]
    public string Kontaktperson_Kontaktnr { get; set; }     
    public string Liefertermin { get; set; }
    [BsonElement("Bearbeiter")]
    public string Bearbeiter { get; set; }
    [BsonElement("Telefon_Bearbeiter")]
    public string Telefon_Bearbeiter { get; set; }
    [BsonElement("Mail_Bearbeiter")]
    public string Mail_Bearbeiter { get; set; }
    [BsonElement("Bestelldatum")]
    public string Bestelldatum { get; set; }
    [BsonElement("Projektleiter")]
    public string Projektleiter { get; set; }
    [BsonElement("Bemerkung_oben")]
    public string Bemerkung_oben { get; set; }
    [BsonElement("Angelegt_am")]
    public string Angelegt_am { get; set; }       
    [BsonElement("Rechnungsnr")]
    public string Rechnungsnr { get; set; }
    [BsonElement("letzteAktualisierung")]
    public string letzteAktualisierung { get; set; }

    [BsonElement("Bestellung")]
    public IList<articles> Bestellung { get; set; }

    public Nachbestellung()
    {

    }      
}

Side Model articles (1 'reorders.Bestellung' consists of multiple 'articles' i.e. datagrid rows!)

public class articles
{   
    [BsonElement("Pos")]
    public string Pos { get; set; }

    [BsonElement("Artikelbezeichnung")]
    public string Artikelbezeichnung { get; set; }

    [BsonElement("Artikelnummer")]
    public string Artikelnummer { get; set; }

    [BsonElement("Einheit")]
    public string Einheit { get; set; }

    [BsonElement("Menge")]
    public string Menge { get; set; }

    [BsonElement("Einzelpreis")]
    public string Einzelpreis { get; set; }

    [BsonElement("Gesamtpreis")]
    public string Gesamtpreis { get; set; }

    [BsonElement("Anforderungsgrund")]
    public string Anforderungsgrund { get; set; }

    [BsonElement("Anforderungsnr")]
    public string Anforderungsnr { get; set; }

    [BsonElement("Anforderer")]
    public string Anforderer { get; set; }

    [BsonElement("Bemerkungen")]
    public string Bemerkungen { get; set; }                    
}

MongoDB structure (see datagrid (last image) for the values that are in there, i had to delete the original ones to protect privacy):

All database entries are here (just imagine they are all filled. I had to remove the actual values due to privacy protection). No problem here! The Bestellung-Array (order-Array) is filled just fine like I want it.

BUT the datagrid view doesnt show the array values starting from "Pos" till "Bemerkungen". The single value-fields are all showing up, though!

CODE BEHIND (is also my ViewModel atm, i know breach of MVVM... but not important right now unless responsible for the array not appearing in the datagrid!)

public existingReorders(string hv)
    {
        InitializeComponent();
        var db = new MongoCRUD("myDB");
        //MongoDB Query
        var recs = db.LoadRecords<reorders>("myReorders", hv);
        //checking if there are any records
        if(recs.Count <= 0)
        {
            MessageBox.Show("There were no records found for this hv!", "NO REORDERS FOUND!", MessageBoxButton.OK, MessageBoxImage.Information);
        }
        else
        {
            this.Show();                
            dgVorh.ItemsSource = recs;  
            dgVorh.DataContext = recs;

        }
    }

XAML:

<Window x:Class="Nachbestellungen.existingReorders"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Nachbestellungen"
    mc:Ignorable="d"
    WindowStartupLocation="CenterScreen"
    Title="Existing Reorders" WindowState="Maximized" WindowStyle="ThreeDBorderWindow">

<Window.Resources>
    <Style x:Key="cellLightGray" TargetType="{x:Type TextBlock}">
        <Setter Property="Background" Value="LightGray" />
    </Style>
</Window.Resources>
<!-- View -->
<Grid x:Name="gridVorh">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <DataGrid x:Name="dgVorh" Margin="5" Grid.Row="0"
              SelectionMode="Single" SelectionUnit="Cell" IsReadOnly="False" 
              CanUserAddRows="False" CanUserDeleteRows="False" AutoGenerateColumns="False" 
              BorderBrush="Black" BorderThickness="2" RowHeight="30" FontFamily="Arial Narrow" FontSize="18">

        <!-- Style Column Headers -->
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="Foreground" Value="#FFFFFF"/>
                <Setter Property="Background" Value="#DD002C"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="BorderThickness" Value="0,0,1,2"/>
                <Setter Property="BorderBrush" Value="Black"/>
                <Setter Property="FontSize" Value="18"/>
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                <Setter Property="Height" Value="30"/>
            </Style>
        </DataGrid.Resources>

        <DataGrid.Columns>
            <DataGridTextColumn Header="Angelegt am" Binding="{Binding Angelegt_am}" IsReadOnly="True">
                <DataGridTextColumn.ElementStyle>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="TextBlock.Background" Value="LightGray"/>
                    </Style>
                </DataGridTextColumn.ElementStyle>
            </DataGridTextColumn>
            <DataGridTextColumn Header="Bearbeiter" Binding="{Binding Bearbeiter}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Bestelldatum" Binding="{Binding Bestelldatum}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Bestellt bei" Binding="{Binding Empfaenger}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Lieferort" Binding="{Binding Anlieferungsort}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Adressat" Binding="{Binding Adressat}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Lieferanschrift" Binding="{Binding Anschrift}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Lieferort" Binding="{Binding Plz_Ort}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Rechnungsnr" Binding="{Binding Rechnungsnr}"/>
            <DataGridTextColumn Header="ÄndDatum" Binding="{Binding letzteAktualisierung}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <!--(von wem aktualisiert?)-->
            <!-- Artikel -->

            <DataGridTextColumn Header="Pos" Binding="{Binding Path=[Bestellung].Pos}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Artikelbezeichnung" Binding="{Binding Path=[Bestellung].Artikelbezeichnung}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Artikelnummer" Binding="{Binding Bestellung}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Einheit" Binding="{Binding Bestellung}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Menge" Binding="{Binding Bestellung}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Einzelpreis" Binding="{Binding Bestellung}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Gesamtpreis" Binding="{Binding Bestellung}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Anforderungsgrund" Binding="{Binding Bestellung}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Anforderungsnr" Binding="{Binding Bestellung}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Anforderer" Binding="{Binding Bestellung}" IsReadOnly="True" ElementStyle="{StaticResource cellLightGray}"/>
            <DataGridTextColumn Header="Bemerkungen" Binding="{Binding Bestellung}"/>
        </DataGrid.Columns>
    </DataGrid>
    <StackPanel Name="stpnlUpdate" Grid.Row="1" HorizontalAlignment="Center">
        <Button Name="btnUpdate" Content="Aktualisieren" 
                Background="#DD002C" Foreground="White" 
                BorderThickness="2" BorderBrush="Black" 
                Click="BtnUpdate_Click" 
                Height="40" Width="130" 
                FontFamily="Verdana" FontStyle="Oblique" 
                FontStretch="ExtraCondensed" FontSize="15" 
                FontWeight="ExtraBlack" 
                MouseEnter="BtnUpdate_MouseEnter" MouseLeave="BtnUpdate_MouseLeave">
        </Button>
    </StackPanel>
</Grid>

This is how I want it to look like: enter image description here

This is how the INSERT view (insert into MongoDB) looks like: (without the single values which are cut off here because they are irrelevant)

enter image description here

timunix
  • 609
  • 6
  • 19
  • I am missing the array values from 'Pos' till 'Bemerkungen'. The rest is working fine because the rest are single values, not array values. What I ideally want: 1 row per reorder inside my datagrid. (I hope my question is understandable!) – timunix Oct 28 '19 at 11:50
  • Can you make the screen shot of your data copiable? – ntohl Oct 28 '19 at 14:22
  • How do you imagine the multiple rows within a row? there would be a cell with an inner `DataGrid`? Or like an SQL join Hv, Bv, ..., Pos, Artikelbezeichnung, ... in one row, and several rows where the Hv, Bv, ... part is duplicated? – ntohl Oct 28 '19 at 14:29
  • @ntohl: where the HV-BV part is duplicated because I need each Pos, Artikelbezeichung in one row. Otherwise I wouldnt be able to UPDATE=Aktualisieren the invoice number (Rechnungsnr.) and/or comments (Bemerkungen)later on for the corresponding position. – timunix Oct 28 '19 at 15:05
  • this way if you want to update the Rechnungsnr. you will have to set the same Rechnungsnr. for each Bestellung. Or may rip out a Bestellung from the array – ntohl Oct 28 '19 at 15:13
  • It should be a master-detail kind of table to be honest – ntohl Oct 28 '19 at 15:15
  • @ntohl: you are right, the rechnungsnr must go into the article class. This is what I need to change but lets assume I have already changed that, what else do I need to do to get the array inside my datagrid as desired? (I EDITED MY POST WITH A PICTURE THAT SHOWS MY DESIRED RESULT) Now, I edited the desired result, each Pos has its own Rechnungsnr (invoice number). – timunix Oct 28 '19 at 15:20
  • @ntohl: The Rechnungsnr goes into the article class. I don't mind if the HV-BV-Bauleiter etc. are duplicates for each row. So if I have more than 1 position I am ok with duplicate lines, too for that reorder. Should I add the view where I type in the values for the reorder as well? – timunix Oct 28 '19 at 15:29
  • 1
    It's not that easy. You will have `AggregatedReorders` each line, and customly make the update/read in the Update. Check out how to make the read> https://stackoverflow.com/a/58485925/1859959 – ntohl Oct 28 '19 at 15:40
  • @ntohl: I thought so, that's why I landed here ;-) . If it is not that easy, am I doing it too complicated? Is there an easier way? (maybe even outside of C#?) How would you do it if you wanted to achieve what I want to? Maybe I am thinking in a too complicated indirect way due to lack of experience? :-) – timunix Oct 28 '19 at 15:53
  • 1
    I would split it into 2 Grids. One capable of setting/updating the HV-BV part. Other one is filled up by the selected line's articles. https://code.msdn.microsoft.com/windowsdesktop/CSWPFMasterDetailBinding-c78566ae https://www.codeproject.com/Articles/332615/WPF-Master-Details-MVVM-Application But it's definitely an XY problem. Your problem is not that array doesn't show in the grid... – ntohl Oct 28 '19 at 15:57
  • @ntohl: Ok, I thought about that option, too but I was scared that the actual data lines might get "decoupled" because of that. Now that you made that proposal, I feel more comfortable giving it a try. Thank you for your time and readiness to help! :-) – timunix Oct 28 '19 at 16:00
  • I will post the solution once I got the hang of it for developers with a similar/the same issue. – timunix Oct 28 '19 at 16:15

0 Answers0