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

时间:2017-05-03 06:38:51

标签: entity-framework linq datagridview linq-to-entities

我是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 个答案:

答案 0 :(得分:0)

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

第1步:为查询结果返回类型定义.NET类型:

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

第2步:将“正常”查询更改为:

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步:如果您要“清除”DataGridView,但保留列,您现在可以使用:

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