如何使用 Entity Framework 作为 DataGridView 的通用数据源?
How to use Entity Framework as generic DataSource for DataGridView?
我正在尝试制作 DataGridView
形式,它采用 generic DataSource
并显示数据。
我无法使数据绑定工作:
An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.dll
Additional information: Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) 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(). For ASP.NET WebForms you can bind to the result of calling ToList() on the query or use Model Binding, for more information see http://go.microsoft.com/fwlink/?LinkId=389592.
我尝试通过 DataTable
、DataSet
和 List
- 但没有成功...我的问题是我想使用存储库模式并创建一个通用存储库 class 它可以与我的数据库中的不同表一起使用。
这是我第一次尝试编写存储库模式代码并使用泛型类型。
public DataGridForm(IEnumerable<???> source) // TEntity or T can't be recognized by VisualStudio as a type!
{
InitializeComponent();
// TODO: Complete member initialization
this.bindingSource.DataSource = source;
}
我很难让这个变得简单。请提供有关如何实现此目标的指南?
我调用这个方法:
public IEnumerable<TEntity> GetAll()
{
return Context.Set<TEntity>().ToList();
}
我知道这是一个很复杂的问题,但我需要某种方式的帮助
~陈迟
更新: 接受的答案没有问题,真正的问题是我的 AutoGenerateColumns 属性 默认设置为 false!
dataGridView.AutoGenerateColumns = true;
通过代码隐藏将其设置为 true 使一切正常...我在 属性 windows 中寻找这个 属性 但我找不到它?!也许是某种我不知道的 Visual Studio 错误...
DataSource
BindingSource
的 属性 接受 object
。因此,将数据作为 object
传递给表单的构造函数就足够了:
public DataGridForm(object source)
{
InitializeComponent();
// TODO: Complete member initialization
this.bindingSource.DataSource = source;
}
对于 run-time data-binding 你不需要类型。但是出于任何原因,如果您想将 IEnumerable<T>
传递给表单,表单应该是 DataGridForm<T>
,这在设计器中有其副作用。
我正在尝试制作 DataGridView
形式,它采用 generic DataSource
并显示数据。
我无法使数据绑定工作:
An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.dll
Additional information: Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) 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(). For ASP.NET WebForms you can bind to the result of calling ToList() on the query or use Model Binding, for more information see http://go.microsoft.com/fwlink/?LinkId=389592.
我尝试通过 DataTable
、DataSet
和 List
- 但没有成功...我的问题是我想使用存储库模式并创建一个通用存储库 class 它可以与我的数据库中的不同表一起使用。
这是我第一次尝试编写存储库模式代码并使用泛型类型。
public DataGridForm(IEnumerable<???> source) // TEntity or T can't be recognized by VisualStudio as a type!
{
InitializeComponent();
// TODO: Complete member initialization
this.bindingSource.DataSource = source;
}
我很难让这个变得简单。请提供有关如何实现此目标的指南?
我调用这个方法:
public IEnumerable<TEntity> GetAll()
{
return Context.Set<TEntity>().ToList();
}
我知道这是一个很复杂的问题,但我需要某种方式的帮助
~陈迟
更新: 接受的答案没有问题,真正的问题是我的 AutoGenerateColumns 属性 默认设置为 false!
dataGridView.AutoGenerateColumns = true;
通过代码隐藏将其设置为 true 使一切正常...我在 属性 windows 中寻找这个 属性 但我找不到它?!也许是某种我不知道的 Visual Studio 错误...
DataSource
BindingSource
的 属性 接受 object
。因此,将数据作为 object
传递给表单的构造函数就足够了:
public DataGridForm(object source)
{
InitializeComponent();
// TODO: Complete member initialization
this.bindingSource.DataSource = source;
}
对于 run-time data-binding 你不需要类型。但是出于任何原因,如果您想将 IEnumerable<T>
传递给表单,表单应该是 DataGridForm<T>
,这在设计器中有其副作用。