0

Hello I have a ObservableCollection that populates from a database.

Here is a print of the data being added to the collection:

7/15/2021 12:00:00 AM
Domestic                      
BENDING                       
BENDER11                      
testing                                                           
173-1  
171-3 SILENCER                                    
Waldo                         
1
[{"PartNum":"0.579x79.60x358.30HFS436L","PartDescription":"TYA A1 CHBR MTL","PartReason":"SPLIT COMP / MID LEFT","PartQty":1.0},{"PartNum":"18308-TJB-A021-Y1","PartDescription":"SILENCER, EXHAUST R","PartReason":"NG Cat Depth/ Misset","PartQty":3.0},{"PartNum":"18231-S10-0030","PartDescription":"BOLT COMP FLEX JOINT"

SQL:
sql

As you can see I store parts as JSON

I have the binding done for the SelectedItem here
Data being binded

<Grid ShowGridLines="False">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="200"/>
                                <ColumnDefinition/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <StackPanel>
                                <TextBlock
                            Text="{Binding SelectedItem.ID, ElementName=listTickets, StringFormat='Ticket ID: {0}'}"
                            FontSize="20"/>
                                <TextBlock Text="{Binding SelectedItem.Date, ElementName=listTickets, StringFormat=MM-dd-yyyy}"
                                       FontSize="15"
                                       Margin="3,5,0,0"/>
                            </StackPanel>
                            <StackPanel  Grid.Column="3" >
                                <TextBlock HorizontalAlignment="Center"
                                           VerticalAlignment="Center"
                                           Text="{Binding SelectedItem.Dept, ElementName=listTickets, StringFormat='Department: {0}'}"
                                           FontSize="20"/>
                                <TextBlock HorizontalAlignment="Center"
                                           VerticalAlignment="Center"
                                           Text="{Binding SelectedItem.Createdby, ElementName=listTickets, StringFormat='Submitted by: {0}'}"
                                           FontSize="15"/>
                            </StackPanel>
                        </Grid>

My question is how can I get Parts from the SelectedItem then Deserialize that and add it into the the DataGrid with columns [PartNum, PartDescription, PartReason, PartQty]

I was trying some code behind stuff but couldn't really get it

Here is what I have tried which is just parsing the data from the selection from codebehind.

private void listTickets_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            JsonData partsJson = JsonSerializer.Deserialize<JsonData>(listTickets.SelectedValue.Parts);
        }
EldHasp
  • 6,079
  • 2
  • 9
  • 24
  • Now you have an object and you can write code to format it for the display. Just do it – Thomas Weller Sep 07 '21 at 15:23
  • @ThomasWeller idk if that was suppose to be helpful or not? If I could "Just do it" then I wouldn't be here asking for help? – WPFAdvocate Sep 07 '21 at 15:36
  • Take partsJson and provide it as DataSource for your DataGrid. DataGrid will parse the property name & property values and will display accordingly. – Lingam Sep 07 '21 at 18:37
  • @Lingam this is the issue I am getting when trying to do that: https://prnt.sc/1ribkwd – WPFAdvocate Sep 07 '21 at 19:26
  • Please check this link: https://stackoverflow.com/questions/3142495/deserialize-json-into-c-sharp-dynamic-object Try to convert your json string into a dynamic object. Then try to add that into a list of object and set that as source. (I haven't tried to recreate the issue. I'm just sharing ideas) – Lingam Sep 07 '21 at 19:39
  • @Lingam thank you I was able to figure it out! – WPFAdvocate Sep 08 '21 at 11:31

1 Answers1

0
private void listTickets_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var item = (ListBox)sender;
    var data = (PendingTickets)item.SelectedItem;
    dynamic stuff = JsonConvert.DeserializeObject(data.Parts);
    dgvReviewParts.ItemsSource = stuff;
}

First you need to get the item of the sender and get the SelectedItem. Then you Deserialize the Object and set that as your DataGrid ItemSource