简体   繁体   中英

Using EF5 to bind to Data Grid View issue

I'm new to using EF and I'm currently using EF5 to attempt to bind data from a view to my data grid view in a WinForms App. I'm not sure how to do this properly, and I'm getting an error:

Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList().

Here is my code:

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;

        }

I don't think I fully understand what the error is directing me to do. I've done some brief searching but I don't think I've found what I need. How is the DbSet supposed to be implemented and used to bind data to DGVs, or is there an easier way?

Can provide me with some insight on how to bind the qry object properly to the data grid view? Right now I will just need to view the data, but going forward I would like to be able to access a row and update it based on ID.

Thanks

Just use the ToList() method on the returned query.

Try to the following:

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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM