Failed to create Gridview by dragging a DataSource object into WinForm

I want to create a set of DataGridView controls by dragging their corresponding DataSources into the form. I would do this for an object and one of its navigation properties (from DataSources), thus having two grids in relation to the "Master-Details".

After the transition to Visual Studio 2012 (and EF 5.0), the first grid will pass as expected (using the navigator). However, for a “granular” grid, only two columns are displayed (“Count” and “IsReadOnly”). I tried this with other tables / objects, and I came across the same problem. I tried "Edit Columns" and "Add Column" so that it was not available. As an example, I use the north wind. I want 2 datagridviews in the form. one for customer and one for related orders. typical view of the wizard / parts.

  • I added a model for the project (edmx).

  • A new data source for modeling has been added. two objects. customers and orders!

  • dragged client object as datagridview. All fields (properties) are in the grid.

4 when dragging orders from the customer’s subject, and not the essence of the order itself, there are only two columns with the names "count" and "readonly"

The 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 the same as the beth massi method described at http://blogs.msdn.com/b/bethmassi/archive/2008/12/10/master-details-with-entity-framework-explicit-load.aspx however this is not a lazy or impatient download problem!

+4
source share
1 answer

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

From this:

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

For this:

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

Since this is automatically generated code, you will also need to change some rules in the code generation file. Try manually making these changes so that they work first and 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 this.

 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 ad to Observable Collection

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

Also change ICollection to ObservableCollection

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

The code generation file may differ from mine, but these parts of the code should give you an idea of ​​what to look for in your file.

+1
source

Source: https://habr.com/ru/post/1437441/


All Articles