[Solved] Data binding directly to a store query (DbSet DbQuery DbSqlQuery) is not supported.

Last Reply 8 months ago By Mudassar

Posted 8 months ago

Hi,

I am getting the following error when binding Entity Framework data to DataGridView.

An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.dll

Additional information: 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().

Code:

private void Form1_Load(object sender, EventArgs e)
{
    NorthwindEntities entities = new NorthwindEntities();
    var customers = from p in entities.Customers
                    select new
                    {
                        CustomerId = p.CustomerID,
                        ContactName = p.ContactName,
                        Country = p.Country
                    };
    dataGridView1.DataSource = customers;
}

You are viewing reply posted by: Mudassar 8 months ago.
Posted 8 months ago

Simply add ToList() to your variable

dataGridView1.DataSource = customers.ToList();