简体   繁体   English

使用LINQ将数据绑定到网格视图,而无需添加DataClasses

[英]Bind data to grid view using LINQ with out adding DataClasses

I have seen many articles using LINQ to bind gridview , but in all are using DataClasses . 我已经看过许多使用LINQ绑定gridview文章,但所有文章都使用DataClasses Is it possible to bind data to grid view using LINQ with out adding DataClasses .. Can any one give me any sample code or working example on this 是否可以在不添加DataClasses使用LINQ将数据绑定到网格视图。.任何人都可以在此给我任何示例代码或工作示例吗

在此处输入图片说明

I think I may be missing the point, but whatever the source of your data, as long as it's some form of collection, you can use Linq to objects to query the data. 我想我可能没有抓住重点,但是无论数据的来源是什么,只要是某种形式的收集,就可以使用Linq来对象查询数据。 So for example: 因此,例如:

public partial class WebUserControl1 : System.Web.UI.UserControl
{
    private readonly List<DataItem> _items;

    public WebUserControl1()
    {
        _items = new List<DataItem>
                     {
                         new DataItem {Name = "Fred"},
                         new DataItem {Name = "Dave"},
                         new DataItem {Name = "John"},
                     };
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        // Get data and use Linq to objects
        var filteredData = GetData().Where(i => i.Name.StartsWith("F")).ToList();

        // Bind to data 
        GridView1.DataSource = filteredData;
        GridView1.DataBind();
    }

    private IEnumerable<DataItem> GetData()
    {
        // Get data from your data source
        return _items;
    }
}

public class DataItem
{
    public string Name { get; set; }
}

If your data is an a database, then a good option is to use Linq to SQL (SQL Server only) or Linq to Entities (not just SQL Server), or Linq to Datasets (if you really have to). 如果您的数据是数据库,那么一个不错的选择是使用Linq to SQL(仅适用于SQL Server)或Linq to Entities(不仅限于SQL Server),或使用Linq to Datasets(如果确实需要)。

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

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