【问题标题】:Databinding LINQ query results (with join tables) to DataGrid将 LINQ 查询结果(带有连接表)数据绑定到 DataGrid
【发布时间】:2011-02-15 17:46:07
【问题描述】:

在我的 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根据查询结果中的行数显示行,但是它们是空白的!

那么我怎样才能对这些数据进行数据绑定并在DataGrid中显示呢?

附:对于更简单的查询,例如select * from _table_,一切正常,DataGrid 显示数据,但是当我对多个表使用更复杂的查询(进行连接)时,我无法正确绑定它。

【问题讨论】:

    标签: wpf linq data-binding datagrid join


    【解决方案1】:

    LINQ join 与 SQL join 不同。它不会将不同的来源合并到一个普通的来源中。 因此,请更改 DataGrid 中的每个绑定,如下例所示:

    {Binding Path=workerID}
    

    {Binding Path=dWorker.workerID}
    

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

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

    【讨论】:

      【解决方案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();
      

      对我来说很好用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多