将LINQ查询结果(带连接表)数据绑定到DataGrid

时间:2011-02-15 17:46:07

标签: wpf linq data-binding datagrid join

在我的WPF 4基于桌面的应用程序中,我有一个LINQ查询,它使用两个表进行连接。

以下是返回LINQ查询结果的函数代码:

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;
}

现在,我要做的就是将此LINQ查询的数据绑定结果发送到DataGrid

private IList currentData; //class property

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

这是DataGrid的XAML代码:

            <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>

调试后我得到了以下结果:

  1. 我的查询返回带结果的行
  2. 我的DataGrid根据查询结果中的行数显示行,但它们是空白的!
  3. 那么我如何数据绑定这些数据并在DataGrid

    中显示它们

    P.S。如果有更简单的查询,例如select * from _table_,一切正常,DataGrid显示数据,但当我使用多个表(make join)的更复杂查询时,我无法正确地对其进行数据绑定。 / p>

2 个答案:

答案 0 :(得分:2)

LINQ加入的工作方式与SQL加入不同。它不会在一个普通源中合并不同的源。 因此,在此示例中更改DataGrid中的每个绑定:

{Binding Path=workerID}

{Binding Path=dWorker.workerID}

d2.workerID,我不知道这些类的属性,但错误在于绑定。

并且不要忘记在IsReadOnly="True"中设置DataGrid,否则如果开始编辑则会引发异常。

答案 1 :(得分:2)

您不必在绑定子句中使用实体别名。只需将列名放在Binding子句

<DataGridTextColumn Width="200" Header="CPF" IsReadOnly="True" Binding="{Binding Path=NumeroDoc}">

从代码中删除.ToList(),返回一个IEnumerable对象

public static IEnumerable getQueryTable()
    {
        var dataList = (from c in App.DbContext.Cliente
                 join d in App.DbContext.DadosDocumento
                 on c.IDCliente equals d.IDCliente
                 select new { c.IDCliente, c.Nome, c.DataCadastro, d.NumeroDoc });
        return dataList;
    }

使用ItemsSource属性填充数据网格:

dgDados.ItemsSource = DBHelper.getQueryTable();

它适用于我。