在一个DataTable prevent为什么叫AsEnumerable()绑定到这一个GridView? [英] Why does calling AsEnumerable() on a DataTable prevent a GridView from binding to it?

查看:173
本文介绍了在一个DataTable prevent为什么叫AsEnumerable()绑定到这一个GridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的.aspx页面中,我有一个< ASP:GridView控件=服务器ID =CustomerGridView> 控件,我绑定是这样的:

In my .aspx page, I have an <asp:GridView runat="server" ID="CustomerGridView"> control which I bind like this:

public partial class InsertCustomer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewCustomer();
        }
    }

    private void ViewCustomer()
    {
        var manager = new DomainManager();
        var result = manager.FindAll(new Customer());
        this.CustomerGridView.DataSource = result;
        this.CustomerGridView.DataBind();
    }
}

下面是 DomainManager 类:

public class DomainManager
{
    readonly StringBuilder _queryBuilder = new StringBuilder();
    private Type _entityType;
    readonly SQLHelper _sqlHelper = new SQLHelper();

    public IEnumerable FindAll<T>(T entity)
    {
        _entityType = entity.GetType();
        var query = string.Format("select * from {0}", _entityType.Name);
        var dt = _sqlHelper.FillDataTable(query);
        return dt.AsEnumerable();
    }
}

的问题是,我的网格没有被正确的约束。为什么不呢?

The problem is that my grid is not being bound correctly. Why not?

推荐答案

试着改变

return dt.AsEnumerable(); 

return dt.DefaultView;

说明:默认情况下,的GridView 绑定到一个对象的实际属性。例如,如果数据源是名单,其中,客户&GT; ,那么你可以绑定到名称每个属性客户。但是当数据源是数据表客户,由psented那么每个客户重新$ P $一个的DataRow ,和的DataRow 做的没有的有名称属性的 GridView控件可以结合。

Explanation: By default, a GridView binds to actual properties of an object. For example, if the data source is a List<Customer>, then you can bind to the Name property of each Customer. But when the data source is a DataTable of customers, then each customer is represented by a DataRow, and a DataRow does not have a Name property that the GridView can bind to.

要支持动态属性,对象必须实现 ICustomTypeDescriptor 接口。该接口由 DataRowView的执行但的DataRow 。通过改变code返回 dt.DefaultView (这是一个数据视图),您所提供的的GridView DataRowView的对象,它可以绑定到。

To support dynamic properties, an object must implement the ICustomTypeDescriptor interface. This interface is implemented by DataRowView but not DataRow. By changing the code to return dt.DefaultView (which is a DataView), you provide the GridView with a collection of DataRowView objects that it can bind to.

现在你可能会奇怪,为什么

Now you might be wondering why

this.CustomerGridView.DataSource = dt;

的作品,但

this.CustomerGridView.DataSource = dt.AsEnumerable();

原因是,数据表实施 IListSource 接口,它告诉的GridView 不要用我作为数据源,用我的默认视图来代替。但 AsEnumerable()返回包装对象,不执行 IListSource ,所以的GridView 不知道怎么去了默认视图

The reason is that DataTable implements the IListSource interface, which tells the GridView "don't use me as the data source, use my DefaultView instead." But AsEnumerable() returns a wrapper object that doesn't implement IListSource, so the GridView doesn't know how to get to the DefaultView.

这篇关于在一个DataTable prevent为什么叫AsEnumerable()绑定到这一个GridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆