0

我是使用 EF 的新手,我目前正在使用 EF5 尝试将数据从视图绑定到 WinForms 应用程序中的数据网格视图。我不确定如何正确执行此操作,并且出现错误:

不支持将数据直接绑定到存储查询(DbSet、DbQuery、DbSqlQuery)。而是使用数据填充 DbSet,例如通过在 DbSet 上调用 Load,然后绑定到本地数据。对于 WPF 绑定到 DbSet.Local。对于 WinForms 绑定到 DbSet.Local.ToBindingList()。

这是我的代码:

using (MyEntities context = new MyEntities ())
        {

            var qry = from col in context.vwSystemProperties
                      select new
                          {
                              SystemPropertyName = col.SystemPropertyName,
                              SystemPropertyEnumVal = col.SystemPropertyEnumVal,
                              SystemPropertyValue = col.SystemPropertyValue,
                              ApplicationScope = col.ApplicationScope,
                              CategoryScope = col.CategoryScope,
                              EntityScope = col.EntityScope,
                              VersionDate = col.VersionDate,
                              VersionUser = col.VersionUser
                          };


            BindingSource bs = new BindingSource();
            bs.DataSource = qry;
            SystemPropertyDGV.DataSource = bs;

        }

我不认为我完全理解错误引导我做什么。我做了一些简短的搜索,但我认为我没有找到我需要的东西。DbSet 应该如何实现并用于将数据绑定到 DGV,或者是否有更简单的方法?

可以为我提供一些有关如何将 qry 对象正确绑定到数据网格视图的见解吗?现在我只需要查看数据,但接下来我希望能够访问一行并根据 ID 更新它。

谢谢

4

1 回答 1

1

只需ToList()在返回的查询上使用该方法。

尝试以下操作:

using (MyEntities context = new MyEntities ())
{

    var qry = from col in context.vwSystemProperties
              select new
                  {
                      SystemPropertyName = col.SystemPropertyName,
                      SystemPropertyEnumVal = col.SystemPropertyEnumVal,
                      SystemPropertyValue = col.SystemPropertyValue,
                      ApplicationScope = col.ApplicationScope,
                      CategoryScope = col.CategoryScope,
                      EntityScope = col.EntityScope,
                      VersionDate = col.VersionDate,
                      VersionUser = col.VersionUser
                  };


    BindingSource bs = new BindingSource();
    bs.DataSource = qry.ToList();
    SystemPropertyDGV.DataSource = bs;

}
于 2013-07-25T14:36:40.933 回答