如何在C#中将两个数据源绑定到DataGrid

时间:2019-04-21 18:21:01

标签: c# wpf entity-framework data-binding datagrid

我的问题如下: 我有一个使用实体框架的“ salesTable”数据网格,我使用以下代码加载该数据网格:

tqdm.auto

salesTable的其中一列是customerId,这是customerTable的外键,现在我想在数据网格中显示customerName而不是customerId,但是我不知道如何。

salesTable如下:

pandas

请帮助!

1 个答案:

答案 0 :(得分:0)

创建此ViewModel:

  public class GridViewModel
    {
        public int SaleId { get; set; }
        public string CustomerName { get; set; }
        //other properties you want
    }

然后在查询中使用GridViewModel:

var result = _db.salesTables.Include(x => x.customerTable)
                            .Select(x => new GridViewModel()
                          {
                               SaleId = x.saleId,
                               CustomerName = x.customerTable.CustomerName
                           }).ToList();

最后:

salesDataGrid.ItemsSource = result;