Unable to create Gridview by dragging Object DataSource to WinForm

天大地大妈咪最大 提交于 2020-01-02 05:18:06

问题


I want to create a set of DataGridView controls by dragging their respective DataSources onto a form. I would do that for an object and one of its navigation properties (from the DataSources), thus having two grids in a Master-Detail relationship.

After moving to Visual Studio 2012 (and EF 5.0), the first grid comes through as expected (with the navigator). However, for the "detail" grid, only two columns appear ("Count" & "IsReadOnly"). I've tried it with other tables/objects and I hit the same problem. I've tried "Edit Columns" & "Add Column" to no available. I use northwind as an example. I want 2 datagridview in a form. one for a customer and another for related orders. a typical master/details view.

  1. I added a model to project (edmx) file.

  2. added a new datasource to model. two objects. customer and orders tables!

  3. dragged customer entity as datagridview. All fields (properties) are in grid.

4 when dragging the orders from customer entity not order entity itself, there are only two column named "count" and "readonly"

Above procedures work fine in linq to sql VS 2012, EF 4.0, .NET 4.5. But NOT for EF 5.0.

and,

The steps are exactly same as the way beth massi described in http://blogs.msdn.com/b/bethmassi/archive/2008/12/10/master-details-with-entity-framework-explicit-load.aspx However this is not an issue of lazy or eager loading!


回答1:


In the class for the entity you are trying to work with change the navigation property type to ObservableCollection.

From this:

public Customer()
    {
        this.CustomerAddresses = new HashSet<CustomerAddress>();
    }

    public virtual ICollection<CustomerAddress> CustomerAddresses { get; set; }
}

To this:

public Customer()
    {
        this.CustomerAddresses = new ObservableCollection<CustomerAddress>();
    }

    public virtual ICollection<CustomerAddress> CustomerAddresses { get; set; }
}

Since this is auto generated code you will also need to change some rules in your code generation file. Try manually making these changes to ensure they work first then make the changes. For me I had to make the following changes to my .tt file

Add using System.Collections.ObjectModel by adding the line below that mentions that.

return inHeader == string.IsNullOrEmpty(_code.VsNamespaceSuggestion())
        ? string.Format(
            CultureInfo.InvariantCulture,
            "{0}using System;{1}" +
            "{2}",
            inHeader ? Environment.NewLine : "",
            includeCollections ? (Environment.NewLine + "using System.Collections.Generic;") : "",
            includeCollections ? (Environment.NewLine + "using System.Collections.ObjectModel;") : "",
            inHeader ? "" : Environment.NewLine)
        : "";

And change your HastSet declaration to Observable Collection

this.<#=code.Escape(navigationProperty)#> = new ObservableCollection<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();

Also, change ICollection here to be ObservableCollection

navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many ? ("ICollection<" + endType + ">") : endType,

Your code generation file may differ from mine but these code parts should give you an idea of what to search for in your file.



来源:https://stackoverflow.com/questions/12695754/unable-to-create-gridview-by-dragging-object-datasource-to-winform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!