清除链接到 Entity Framework 的 DataGridView 数据源

Clear DataGridView datasource linked to Entity Framework

我是 Entity Framework 的新手,我有一个 DataGridView 连接到 Entity Framework,如下所示

dgvDebit.DataSource = (from daily in accountingEntities.DailyAccounts
                       join voucherdetails in accountingEntities.VoucherDetails on daily.DailyId equals voucherdetails.DailyId
                       where voucherdetails.VoucherId == keyvalue
                       group voucherdetails by daily.DailyName into dgroup
                       select new
                              {
                                  DailyName = dgroup.Key,
                                  SumOfDebit = dgroup.Sum(s => s.Debit)
                              }).ToList();

我的问题是:我想清除 DataGridView 数据源,但我所做的每件事都失败了 - 请帮忙吗?

好的,所以您想绑定到您拥有的类型的空列表。

步骤 1:为您的查询结果定义一个 .NET 类型 return 类型:

public class ResultDTO
{
    public string DailyName { get; set; }
    public decimal SumOfDebit { get; set; }
}

第 2 步:将您的 "normal" 查询更改为:

dgvDebit.DataSource = (from daily in accountingEntities.DailyAccounts
                       join voucherdetails in accountingEntities.VoucherDetails on daily.DailyId equals voucherdetails.DailyId
                       where voucherdetails.VoucherId == keyvalue
                       group voucherdetails by daily.DailyName into dgroup
                       select new ResultDTO
                              {
                                  DailyName = dgroup.Key,
                                  SumOfDebit = dgroup.Sum(s => s.Debit)
                              }).ToList();

第 3 步:如果您想 "clear" DataGridView,但保留列,您现在可以使用:

dgvDebit.DataSource = new List<ResultDTO>();