DataGrid bindings to a list of complex dataTypes WPF(xaml)

One07k-4914 101 Reputation points
2020-07-26T06:05:12.157+00:00

Hello,

I have class contains List as Property that contains another class object as its property.

Please find the class structure below which i have to bind to a datagrid/any control in wpf.

public class EmployeeDetails
{
    /// <summary>
    /// 
    /// </summary>
    public string EmpID { get; set; }

    /// <summary>
    /// 
    ///
    /// </summary>
    public List<Employee> Employee { get; set; }
}
public class Employee
{
    /// <summary>
    /// 
    /// </summary>
    public string Id { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public Department Department { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public uint Priority { get; set; }
}

public class Department
{


    /// <summary>
    /// 
    /// </summary>
    public uint ContactNo { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public decimal Salary { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public decimal Allowance { get; set; }

    /// <summary>
    /// 
    /// </summary>
    public JobType JobType { get; set; }

}
public enum JobType
{
   Permanant,
    Temporary
}

Actually my model contains these much data.I have to bind these much information into a dataGrid using xaml binding attribute.

Can anyone help me to solve this???

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,681 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Peter Fleischer (former MVP) 19,231 Reputation points
    2020-07-26T07:30:17.253+00:00

    Hi,
    try following demo. Posting sharp code is prohibited by forum software. Please open attached file with ViewModel and classes.

    <Window x:Class="WpfApp1.Window62"  
            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:WpfApp62"  
            mc:Ignorable="d"  
            Title="Window62" Height="450" Width="800">  
      <Window.DataContext>  
        <local:ViewModel/>  
      </Window.DataContext>  
      <Grid DataContext="{Binding View}">  
        <DataGrid ItemsSource="{Binding Employee}" AutoGenerateColumns="False">  
          <DataGrid.Columns>  
            <DataGridTextColumn Header="ID" Binding="{Binding Id}"/>  
            <DataGridTextColumn Header="Priority" Binding="{Binding Priority}"/>  
            <DataGridTextColumn Header="ContactNo" Binding="{Binding Department.ContactNo}"/>  
            <DataGridTextColumn Header="Salary" Binding="{Binding Department.Salary}"/>  
            <DataGridTextColumn Header="Allowance" Binding="{Binding Department.Allowance}"/>  
            <DataGridTextColumn Header="JobType" Binding="{Binding Department.JobType}"/>  
          </DataGrid.Columns>  
        </DataGrid>  
      </Grid>  
    </Window>  
    

    ----------------------------------------

    13801-x.jpg
    13677-x.txt

    2 people found this answer helpful.
    0 comments No comments

  2. DaisyTian-1203 11,616 Reputation points
    2020-07-28T03:41:14.46+00:00

    You three tables are related, you can use DataGrid.RowDetailsTemplate to implement DataGrid in DataGrid.There is also a doc: How to: Add Row Details to a DataGrid Control for you to refer to.The Xaml code like below:

    13944-2020-07-28-113954.jpg

    1 person found this answer helpful.
    0 comments No comments