结合实体框架对象一个DataGridView C# [英] Binding Entity Framework objects to a Datagridview C#

查看:292
本文介绍了结合实体框架对象一个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();
    }



但我不希望看到所有列在我的数据库表在我的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 }

但我不。知道为什么

推荐答案

与行的问题:

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

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