Search code examples
wpflinqdata-bindingdatagridjoin

Databinding LINQ query results (with join tables) to DataGrid


In my WPF 4 desktop-based application, I have an LINQ query that makes a join with two tables.

Here is code for function that returns results of LINQ query:

public IList GetTableData()
{
    IList dataList = (from dWorker in App.glidusContext.tbl_workers
        join d2 in App.glidusContext.tbl_workers_has_tbl_events
        on dWorker.workerID equals d2.workerID
        where dWorker.workerTZ == strSplit
        select new { dWorker, d2 }).ToList();
    return data list;
}

Now, all what I rest to do is data bind results of this LINQ query to DataGrid:

private IList currentData; //class property

public void AssignTableContentToDataContext() {
    currentData = GetTableData();
    this.ContentDataGrid.DataContext = currentData;
}

And here is an XAML-code of DataGrid:

<DataGrid x:Name="ContentDataGrid"
          Style="{StaticResource Body_Content_DataGrid}"
          CellStyle="{StaticResource Body_Content_DataGrid_Centering}"
          ItemsSource="{Binding}"
          KeyboardNavigation.TabIndex="8"
          SelectionChanged="ContentDataGrid_SelectionChanged" DataContext="{Binding}">
    <!--  PreviewKeyDown="DBRecord_Delete_PreviewKeyDown" -->
    <!--  CellEditEnding="ContentDataGrid_CellEditEnding" -->
    <DataGrid.Columns>
        <DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_WorkerID}"
                            Width="130"
                            IsReadOnly="True"
                            Binding="{Binding Path=workerID}" />
        <DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_workerTZ}"
                            Width="130"
                            Binding="{Binding Path=workerTZ}" />
        <DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_WorkerFirstName}"
                            Width="130"
                            Binding="{Binding Path=workerFirstName}" />
        <DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_WorkerLastName}"
                            Width="130"
                            Binding="{Binding Path=workerLastName}" />
        <DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_EventID}"
                            Width="130"
                            Binding="{Binding Path=eventID}" />
        <DataGridTextColumn Header="{x:Static res:Resources.WinSafetyByEmployee_DataGrid_ColHead_EventName}"
                            Width="*"
                            Binding="{Binding Path=eventName}" />
    </DataGrid.Columns>
</DataGrid>

After debug I got the following results:

  1. My query returns rows with results
  2. My DataGrid shows lines, according to number of lines in the query result, but they are blank!

How can I data bind this data and show them in DataGrid?

P.S. In case of more simple query such as select * from _table_ everything works fine and DataGrid shows data, but when I use more complex queries with more than one table (make join) I can't databind it correctly.


Solution

  • LINQ-join works different from SQL-join. It doesn't merge different sources in one plain source. So change each binding in the DataGrid like in this example:

    {Binding Path=workerID}
    

    for

    {Binding Path=dWorker.workerID}
    

    Or d2.workerID, I don't know the properties of these classes, but the mistake is in the bindings.

    And don't forget to set IsReadOnly="True" in the DataGrid, because otherwise it will raise an exception if to start editing.