简体   繁体   English

使用 C# 和实体框架在 ASP.NET 中加载 GridView

[英]Load GridView in ASP.NET using C# and Entity Framework

I tried loading a DataGridView with data from server and the code I wrote is saying the data source is null during debugging.我尝试使用来自服务器的数据加载 DataGridView,我编写的代码在调试期间说数据源是 null。

protected void ViewData_Load(object sender, EventArgs e)
{
    using (var files = new DBEntitiesModelConn())
    {
        var ViewData= from i in files.LicenseApplicationCPs
                              select new
                                  {
                                      Name = i.Name,
                                      Status = i.Status,
                                      Date = i.DateSubmitted,
                                  };
        ViewDataGrid.DataSource = ViewData.ToList();
    }
}

The datasource is saying is null.数据源是 null。

In ASP.Net, it's often important to have certain data available and ready at certain points in the page life cycle, and not before.在 ASP.Net 中,在页面生命周期的特定时间点(而不是之前)让特定数据可用并准备好通常很重要。 For example, you may need to bind to a drop down list early to allow setting the selected index on that list later.例如,您可能需要尽早绑定到下拉列表,以便稍后在该列表上设置选定的索引。 Or you might want to wait a bit to bind that large grid to reduce the amount of time you hold that connection active/keep the data in memory.或者您可能需要稍等片刻以绑定该大网格以减少您保持该连接处于活动状态/将数据保留在 memory 中的时间。

Having you explicitly call the .DataBind() method makes it possible to support scenarios at both ends of the spectrum.让您显式调用.DataBind()方法可以支持两端的场景。

protected void ViewData_Load(object sender, EventArgs e)
{
    using (var files = new DBEntitiesModelConn())
    {
        var ViewData= from i in files.LicenseApplicationCPs
                              select new
                                  {
                                      Name = i.Name,
                                      Status = i.Status,
                                      Date = i.DateSubmitted,
                                  };
        ViewDataGrid.DataSource = ViewData.ToList();
        ViewDataGrid.DataBind();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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