简体   繁体   English

将实体框架对象绑定到Datagridview C#

[英]Binding Entity Framework objects to a Datagridview C#

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. 我一直在尝试将一个Entity Framework对象绑定到一个DataGridView,但我一直在追求死胡同,我似乎无法在任何地方找到我的答案。

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: 我可以将整个表(实体)绑定到gridview,它将允许我进行更改并将这些更改保存回DB,如下所示:

    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();
    }

but now I cant edit any data in my DataGridView. 但现在我无法编辑DataGridView中的任何数据。

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. 我不能在这里看到木头的树木 - 请有人介意指出我的方式的错误或告诉我将EF与Winforms绑定的最佳实践是什么,因为我正在耗尽人才。

I can see it's to do with the section: 我可以看到它与该部分有关:

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

But I dont know why. 但我不知道为什么。

The problem with the line: 该行的问题:

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

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. 这就是为什么您的更改不会反映在新类型或原始EF对象中的原因。

Now, as for ways of not showing all the columns of the object you are binding to, I've given three options below. 现在,至于没有显示您绑定的对象的所有列的方法,我在下面给出了三个选项。

Hide unwanted columns 隐藏不需要的列

The most straight forward approach is to set the visible property to false for the columns you do not want to show. 最直接的方法是将您不想显示的列的visible属性设置为false。

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. 其中Columns集合索引器中的值可以是指定列位置的整数,也可以是列名称的字符串。

Custom object in EF for this databinding EF中的自定义对象用于此数据绑定

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. 您也可以在EF层处理此问题 - 为您的绑定创建一个自定义对象,EF从数据库映射而不使用您不想要的列。 I haven't used EF 4.0 at all really but I understand that it has this capability now. 我根本没有使用EF 4.0,但我知道它现在有这个功能。

Custom DTO projected from EF object and then mapped back 自定义DTO从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. 第三种选择(在我看来,这些选择从好到坏,但我想我会告诉你一些方法!)是查询具体类型然后映射回EF对象。 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... 在某些情况下,这也可能是一个可行的解决方案......

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

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