2
我是相當新的實體框架和LINQ。我之前使用過DataGridView並設置了它的數據源的DataBind()在C#
DataGridView1.datasource=dt; // dt=datatable
就夠了。但是在讀ASP.Net的書,他們已經給出了使用實體框架的代碼
using (PlanetWroxEntities myEntities = new PlanetWroxEntities())
{
var allGenres = from genre in myEntities.Genres
orderby genre.Name
select new { genre.Name, genre.Reviews };
GridView1.DataSource = allGenres;
GridView1.DataBind();
}
這是爲什麼DataBind()
在末尾。我見過的MSDN文檔DataBind()
它說「(這)將數據源綁定到調用的服務器控件及其所有子控件。」
如果我刪除它,我得到一個錯誤,因爲「該ObjectContext實例已被處置,不能再用於需要連接的操作。」
所以我只是困惑的是,這是什麼DataBind()
實際上呢?我看到一些人也使用DataBind for DataGridView,我不知道爲什麼?
謝謝。