简体   繁体   English

ASP.Net和实体框架筛选器搜索到Gridview

[英]ASP.Net and Entity Framework Filter Search to Gridview

I am working on ASP.NET application which is using Entity Framework and getting data from a Database. 我正在使用ASP.NET应用程序,该应用程序使用实体框架并从数据库中获取数据。 I have following code to filter rendering data on a Grid View. 我有以下代码可以过滤网格视图上的渲染数据。 I tried this code which it wrong for sure! 我尝试过此代码,肯定是错误的!

 protected void btnSearch_Click(object sender, EventArgs e)
        {
            GISEntities gis = new GISEntities();
            GIS_CONTRACTOR_TEST tbl = gis.GIS_CONTRACTOR_TEST.ToList().Where(x => x.CONTRACTORNAME == txtSearch.Text).First();
            GridView1.DataSource = tbl.CONTRACTORNAME;
            GridView1.DataBind();
        }

As you can see I have problem on GridView1.DataSource = tbl.CONTRACTORNAME; 如您所见,我在GridView1.DataSource = tbl.CONTRACTORNAME;GridView1.DataSource = tbl.CONTRACTORNAME;问题GridView1.DataSource = tbl.CONTRACTORNAME; which I couldn't find any other property for tbl except of field constructors. 除了字段构造函数外,我找不到tbl其他任何属性。 Can you please let me know how I can filter the database into a grid View instead of displaying them separately! 您能告诉我如何将数据库过滤到网格视图中,而不是分别显示它们吗?

Thanks 谢谢

If I was to assume GISEntities is your database context then try this. 如果我假设GISEntities是您的数据库上下文,请尝试此操作。

var result = (from a in gis.GIS_CONTRACTOR_TEST where a.CONTRACTORNAME == txtSearch.Text select a).ToList();

GridView1.DataSource = result;
GridView1.DataBind();

If you're not finding the correct properties in the result variable I would take a look at your GIS_CONTRACTOR_TEST model and database context, making sure it is defined correctly. 如果在result变量中找不到正确的属性,我将查看您的GIS_CONTRACTOR_TEST模型和数据库上下文,确保已正确定义。

try this 尝试这个

GISEntities gis = new GISEntities();
GridView1.DataSource = gis.GIS_CONTRACTOR_TEST.Where(m => m.CONTRACTORNAME == txtSearch.Text).ToList();
GridView1.DataBind();

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

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