Search code examples
c#linqdatagridwpfdatagriddatacontext

WPF Datagrid has data in the rows but not displaying text


Maybe it's late and I have been looking at my screen for too long but this really has me stumped. I had the datagrid displaying the data but after I joined 2 tables and tried to display the data. The data is there because I am able to still get the mouse double click event to work but no text is showing in any of the columns or rows.

This is the XAML.

<DataGrid Grid.Column="1" Grid.Row="1" Name="ProjectsSubGrid" ItemsSource="{Binding}" 
          AutoGenerateColumns="False" IsReadOnly="True"  
          MouseDoubleClick="ProjectsSubGrid_MouseDoubleClick" 
          Initialized="ProjectsSubGrid_Initialized">
   <DataGrid.Columns>
      <DataGridTextColumn Binding="{Binding Path=ProjectName}" Header="Project Name"/>
      <DataGridTextColumn Binding="{Binding Path=AllottedHours}" Header="Allotted Hours"/> 
      <DataGridTextColumn Binding="{Binding Path=InvoicedHours}" Header="Invoiced Hours"/>
      <DataGridTextColumn Binding="{Binding Path=UninvoicedHours}" Header="Uninvoiced Hours"/>
      <DataGridTextColumn Binding="{Binding Path=RemainingHours}" Header="Remaining Hours"/>
  </DataGrid.Columns>
</DataGrid>

and this is my C# code that is setting the DataContext.

private void ProjectsSubGrid_Initialized(object sender, EventArgs e)
{
    var list = from p in ProjectManagement.Context.Project
               join a in ProjectManagement.Context.Account
               on p.AccountId equals a.AccountId
               select new ProjectView(){
                   AccountId = a.AccountId,
                   ProjectId = p.ProjectId,
                   ProjectName = p.ProjectName,
                   InvoicedHours = p.InvoicedHours,
                   AccountName = a.CompanyName,
                   AllottedHours = p.AllottedHours,
                   RemainingHours = p.RemainingHours,
                   UninvoicedHours = p.UninvoicedHours
               };
     ProjectsSubGrid.DataContext = list;
}

It works when I use the code below but as soon as I switch to the joined tables it breaks.

ProjectsSubGrid.DataContext = ProjectManagement.Context.Project.Where(p=>true);

Any help would be greatly appreciated.


Solution

  • Thanks everyone for your help here. I guess it was just me looking at the screen for too long last night. I hadn't posted the code for the ProjectView class and that is where the problem was.

    I had written

    public class ProjectView
    {
        public int ProjectId;   
        public string ProjectName;
        public int AccountId;
        public string AccountName;
        public int AllottedHours;
        public int InvoicedHours;
        public int UninvoicedHours;
        public int RemainingHours;
    }
    

    I fixed it by changing this to

    public class ProjectView
    {
        public int ProjectId { get; set; }    
        public string ProjectName { get; set; }
        public int AccountId { get; set; }
        public string AccountName { get; set; }
        public int AllottedHours{ get; set; }
        public int InvoicedHours { get; set; }
        public int UninvoicedHours { get; set; }
        public int RemainingHours { get; set; }
    }