我是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
数据源,但我所做的每件事都失败了 - 请在这里有任何帮助吗?
答案 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>();