Wpf 将LINQ查询结果(使用联接表)数据绑定到DataGrid
在我的基于WPF4桌面的应用程序中,我有一个LINQ查询,它与两个表进行连接 下面是返回LINQ查询结果的函数的代码:Wpf 将LINQ查询结果(使用联接表)数据绑定到DataGrid,wpf,linq,data-binding,datagrid,join,Wpf,Linq,Data Binding,Datagrid,Join,在我的基于WPF4桌面的应用程序中,我有一个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.workerI
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>
调试后,我得到了以下结果:
DataGrid
根据查询结果中的行数显示行,但它们为空李>
那么,如何绑定这些数据并在DataGrid
中显示它们呢
另外,如果是更简单的查询,例如
select*from\u table\u
一切正常,并且DataGrid
显示数据,但是当我对多个表使用更复杂的查询(进行连接)时,我无法正确地对其进行数据绑定。LINQ
连接的工作原理不同于SQL
连接。它不会将不同的源合并到一个普通源中。
因此,更改数据网格中的每个绑定,如本例所示:
{Binding Path=workerID}
为了
或者d2.workerID
,我不知道这些类的属性,但错误在于绑定
不要忘记在数据网格中设置IsReadOnly=“True”
,否则如果开始编辑,它将引发异常。您不必在绑定子句中使用实体别名。只需将列名放在Binding子句上
<DataGridTextColumn Width="200" Header="CPF" IsReadOnly="True" Binding="{Binding Path=NumeroDoc}">
并使用ItemsSource属性填充数据网格:
dgDados.ItemsSource = DBHelper.getQueryTable();
这对我来说很好
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;
}
dgDados.ItemsSource = DBHelper.getQueryTable();