Reputation: 56
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.
Upvotes: 0
Views: 3318
Reputation: 56
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; }
}
Upvotes: 3
Reputation: 10849
1) You should only set the DataContext
of DataGrid
in LoadGrid
method.
this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
ProjectsSubGrid.DataContext = list;
}));
2) It does not make sense to set the Name property of DataGridTextColumn
.
3) Update you linq query as
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
}).ToList();
Upvotes: 0
Reputation: 175
Try adding Path to your binding DataGridTextColumn statement. Here's an example from an previous project of mine.
<DataGrid Grid.Column="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
ItemsSource="{Binding Addresses}"
AutoGenerateColumns="False"
CanUserAddRows="False"
IsReadOnly="True"
SelectionMode="Single"
IsEnabled="{Binding IsSelectionEnabled}"
SelectedItem="{Binding SelectedAddress}"
SelectedIndex="{Binding SelectedIndex}"
>
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Street}" Header="Street"/>
<DataGridTextColumn Binding="{Binding Path=Number}" Header="Number"/>
<DataGridTextColumn Binding="{Binding Path=PostalCode}" Header="PostalCode"/>
<DataGridTextColumn Binding="{Binding Path=City}" Header="City"/>
<DataGridTextColumn Binding="{Binding Path=Provence}" Header="Provence"/>
<DataGridTextColumn Binding="{Binding Path=Country}" Header="Country"/>
</DataGrid.Columns>
</DataGrid>
Upvotes: 0