将实体框架对象绑定到Datagridview C# [英] Binding Entity Framework objects to a Datagridview C#

查看:227
本文介绍了将实体框架对象绑定到Datagridview C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将一个Entity Framework对象绑定到一个DataGridView,但是我不停地击中死点,我似乎无法在任何地方找到我的答案。

I have been trying to bind an Entity Framework object to a DataGridView but I keep hitting dead ends and I can't seem to find my answer anywhere.

可以将整个表(实体)绑定到gridview,它将允许我进行更改并将这些更改保存回数据库,如下所示:

I can bind the whole of a table (entity) to a gridview and it will allow me to make changes and save those changes back to the DB like this:

    WS_Model.WS_Entities context;

    private void simpleButton1_Click(object sender, EventArgs e)
    {
        context = new WS_Entities();

        var query = from c in context.Users select c;

        var users = query.ToList();

        gridControl1.DataSource = users;
    }

    private void simpleButton2_Click(object sender, EventArgs e)
    {
        context.SaveChanges();
    }

但我不想看到我DB中的表中的所有列在我的datagridview中,所以我尝试这样做...

but I dont want to see all of the columns from the table in my DB in my datagridview so I tried doing it this way...

WS_Entities context = new WS_Entities();

    private void simpleButton1_Click(object sender, EventArgs e)
    {
        var query = from c in context.Users
                    where c.UserName == "James"
                    select new { c.UserName, c.Password, c.Description };

        var results = query.ToList();

        gridControl1.DataSource = results;
    }

    private void simpleButton2_Click(object sender, EventArgs e)
    {
        context.SaveChanges();
    }

但现在我无法编辑DataGridView中的任何数据。

but now I cant edit any data in my DataGridView.

我看不到这里的树木,请别人介意指出我们的方式的错误,或者告诉我最终的做法是如何与Winforms绑定EF导致人才流失。

I can't see the wood for the trees here - please would someone mind pointing our the error of my ways or telling me what the best practises are for binding EF with Winforms as I'm getting brain drain.

我可以看到它与该部分有关:

I can see it's to do with the section:

select new { c.UserName, c.Password, c.Description }

但是我没有知道为什么。

But I dont know why.

推荐答案

线路出现问题:

select new { c.UserName, c.Password, c.Description }

是否正在创建匿名类型,匿名类型为不变的 - 这是只读的。这就是为什么您的更改不会反映在新类型或原始EF对象中。

Is that it is creating an anonymous type, and anonymous types are immutable - that is read only. This is why your changes do not get reflected in either the new type or in the original EF object.

现在,关于不显示对象的所有列的方式您有约束力,我已经在下面提供了三个选项。

Now, as for ways of not showing all the columns of the object you are binding to, I've given three options below.

隐藏不需要的列

最直接的方法是将您不想显示的列的visible属性设置为false。

The most straight forward approach is to set the visible property to false for the columns you do not want to show.

dataGridView1.Columns[0].Visible = false;

列集合索引器中的值可以是指定列位置的整数或字符串列的名称。

Where the value in the Columns collection indexer can either be an integer specifying the column location or a string for the column's name.

此数据绑定的EF中的自定义对象

您可以也在EF层处理这一点 - 为您的绑定创建一个自定义对象,EF从数据库映射,而不需要您不需要的列。我没有使用EF 4.0,但我明白现在有这样的功能。

You could also handle this at the EF layer - creating a custom object for your binding which EF maps from the database without the columns you don't want. I haven't used EF 4.0 at all really but I understand that it has this capability now.

自定义DTO从EF对象投影,然后映射回

Custom DTO projected from EF object and then mapped back

第三个选项(这些都是从好的到坏的,在我看来他们,但我以为我会告诉你一些方法!)是查询到具体类型,然后映射回EF对象。类似于:

The third option (and these are going from good to bad in my opinion of them but I thought I'd tell you a few approaches!) is to query to a concrete type and then map back to the EF object. Something like:

private class DataBindingProjection
{
    public string UserName { get; set; };
    public string Password { get; set; };
    public string Description { get; set; };
}

private void simpleButton1_Click(object sender, EventArgs e)
{
    context = new WS_Entities();
    var query = from c in context.Users
                where c.UserName == "James"
                select new DataBindingProjection { UserName = c.UserName, Password = c.Password, Description = c.Description };
    var users = query.ToList();
    gridControl1.DataSource = users;
}

private void simpleButton2_Click(object sender, EventArgs e) 
{
    // and here you have some code to map the properties back from the 
    // projection objects to your datacontext

    context.SaveChanges();
}

在某些情况下,这可能是一个可行的解决方案...

In certain situations that could be a workable solution too...

这篇关于将实体框架对象绑定到Datagridview C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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