1
votes

Of course I have searched everywhere on the web but I can't find the solution of my problem.

My problem

I have Patient Class that contains a list of Audiograms (Audiogramm Class). Actually, I can display list of Patients in the DataGrid. But I want to display, for each patient, his Audiogram in a DataGrid (WPF).

class Patient
{
    public Patient(string patientid, string genre, string createdate)
    {
        this.patientID = patientid;
        this.genre = genre;
        this.createDate = createdate;
        audiogram = new List<Audiogram>();
    }

    public string patientID { get; set; }
    public string genre{ get; set; }       
    public string createDate { get; set; }

    public List<Audiogram> audiogram { get; set; }
}

class Audiogram
{
    public Audiogram(string typeData, string actionDate)
    {
        this.typeData = typeData;
        this.actionData = actionData;
    }

    public string typeData { get; set; }
    public string actionData { get; set; }
}

XmlNodeList nodeList = root.SelectNodes("/pt:NOAH_Patients_Export/pt:Patient/pt:Patient", nsmgr);

foreach (XmlNode node in nodeList)
{
    XmlNodeList nodeListAudio = node.SelectNodes("//pt:Actions", nsmgr);          

    Patient patient = new Patient(node["pt:NOAHPatientId"].InnerText, node["pt:Gender"].InnerText, node["pt:CreateDate"].InnerText);

    foreach (XmlNode nodeAudio in nodeListAudio)
    {
        Audiogram audiogramme = new Audiogram(nodeAudio["pt:TypeOfData"].InnerText, nodeAudio["pt:ActionDate"].InnerText);
        patient.audiogram.Add(audiogramme);                         
    }
    listPatient.Add(patient);               
}
dataGrid_XML.ItemsSource = listPatient;
<DataGrid x:Name="dataGrid_XML" AutoGenerateColumns="false" ItemsSource="{Binding Patient}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding patientID}"  Header="ID Patient"/>
        <DataGridTextColumn Binding="{Binding genre}"  Header="Genre"/>               
        <DataGridTextColumn Binding="{Binding createDate}"  Header="Date création"/>
        <DataGridTextColumn Binding="{Binding audiogram.typeData}"  Header="Type de données"/>
        <DataGridTextColumn Binding="{Binding audiogram.actionData}"  Header="Date de dernière action"/>
    </DataGrid.Columns>
</DataGrid>
3

3 Answers

0
votes

You should go with RowDetails Template

<DataGrid Name="dgUsers" AutoGenerateColumns="False">
    <DataGrid.Columns>
            <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
            <DataGridTextColumn Header="Birthday" Binding="{Binding Birthday}" />
    </DataGrid.Columns>
    <DataGrid.RowDetailsTemplate>
            <DataTemplate>
                    <TextBlock Text="{Binding audiogram.actionData}" Margin="10" />
            </DataTemplate>
    </DataGrid.RowDetailsTemplate>
</DataGrid>
0
votes

why you set your itemssource twice? dataGrid_XML.ItemsSource = listPatient; and ItemsSource="{Binding Patient}"? and your xaml did not work because a List has no property typeData. :) its your item within your list which has this property.

so if you wanna bind your audiogram collection you need some kind of itemscontrol. if you wanna do all in one datagrid check out RowDetailsTemplate.

if you wanna have your mastergrid and a second list for eg your selected row, try

<DataGrid AutoGenerateColumns="false" ItemsSource="{Binding ElementName=dataGrid_XML, Path=SelectedItem.audiogram}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding typeData}"  Header="Type de données"/>
            <DataGridTextColumn Binding="{Binding actionData}"  Header="Date de dernière action"/>
   </DataGrid.Columns>
</DataGrid>
0
votes

I've found one working solution, you made me think to datagrid.items.add()

I've one question, how to delete an empty cell (the first cell) of typeData and actionData column ?

C#

foreach (XmlNode node in nodeList)
{
    XmlNodeList nodeListAudio = node.SelectNodes("//pt:Actions", nsmgr);           

    Patient patient = new Patient(node["pt:NOAHPatientId"].InnerText, node["pt:Gender"].InnerText, node["pt:CreateDate"].InnerText);
    dataGrid_XML.Items.Add(patient);
    foreach (XmlNode nodeAudio in nodeListAudio)
    {
        Audiogram audiogramme = new Audiogram(nodeAudio["pt:TypeOfData"].InnerText, nodeAudio["pt:ActionDate"].InnerText);                   
        dataGrid_XML.Items.Add(audiogramme);
    }              
}

XAML

<DataGrid x:Name="dataGrid_XML" AutoGenerateColumns="false">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding patientID}"  Header="ID Patient" IsReadOnly="True"/>
        <DataGridTextColumn Binding="{Binding genre}"  Header="Genre" IsReadOnly="True"/>
        <DataGridTextColumn Binding="{Binding createDate}"  Header="Date création" IsReadOnly="True"/>
        <DataGridTextColumn Binding="{Binding typeData}"  Header="Type de données" IsReadOnly="True"/>
        <DataGridTextColumn Binding="{Binding actionData}"  Header="Date de dernière action" IsReadOnly="True"/>
    </DataGrid.Columns>        
</DataGrid>