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

13
  • 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!) Commented Oct 28, 2019 at 11:50
  • Can you make the screen shot of your data copiable? Commented Oct 28, 2019 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? Commented Oct 28, 2019 at 14: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> stackoverflow.com/a/58485925/1859959 Commented Oct 28, 2019 at 15:40
  • 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. code.msdn.microsoft.com/windowsdesktop/… codeproject.com/Articles/332615/… But it's definitely an XY problem. Your problem is not that array doesn't show in the grid... Commented Oct 28, 2019 at 15:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.