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

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

问题描述

我一直在尝试将一个实体框架对象绑定到一个 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();
    }

但我不想在我的数据网格视图中看到我的数据库中表中的所有列,所以我尝试这样做...

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.

我在这里看不到树木的木材 - 请有人介意指出我们的错误,或者告诉我在我的人才流失时将 EF 与 Winforms 结合的最佳做法是什么.

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 }

但我不知道为什么.

推荐答案

线路问题:

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.

隐藏不需要的列

最直接的方法是将您不想显示的列的可见属性设置为 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;

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

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 对象投射然后映射回来

第三个选项(在我看来,这些选项从好到坏,但我想我会告诉你一些方法!)是查询到一个具体的类型,然后映射回 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天全站免登陆