2

In Entity Framework When I add Drag from Datasource the grid view only shows Count & Is Read Only Column, I have also tried manually assigning datsource but still not working.

  • Please provide the code that you have tried and are having difficulty with. What is the output that you are getting and what are you looking for? – Matt Whipple Nov 01 '12 at 17:06

2 Answers2

4

Through some researh I have found a solution.

BTW I am using code first.

In the parent entity I changed the child property from list to ObservableCollection. I also added the namespace System.Collections.ObjectModel to my class.

Old:Public Overridable Property PageElements As List(Of PageElement)

New:Public Overridable Property PageElements As ObservableCollection(Of PageElement)

Delete your existing datasource (and controls from the form). Then recreate your datasource drag to the form.

You may need to create a class called ObservableListSource and use that instead of ObservableCollection, but it seems to have cleared up the original problem so far.

Here a suggested definition for the ObservableListSource I found elsewhere on the web.

Public Class ObservableListSource(Of T As Class)
     Inherits ObservableCollection(Of T)
     Implements IListSource
     Private _bindingList As IBindingList

    Private ReadOnly Property ContainsListCollection() As Boolean Implements IListSource.ContainsListCollection
         Get
             Return False
         End Get
     End Property

    Private Function GetList() As IList Implements IListSource.GetList
         Return If(_bindingList, (InlineAssignHelper(_bindingList, Me.ToBindingList())))
     End Function
     Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
         target = value
         Return value
     End Function
EndCLass
Datadude67
  • 41
  • 2
3

This works for me (EF6):

Add an ObservableListSource class to the project and change namespace

using System.Collections; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Diagnostics.CodeAnalysis; 
using System.Data.Entity; 

namespace WinApp 
{ 
public class ObservableListSource<T> : ObservableCollection<T>, IListSource 
    where T : class 
{ 
    private IBindingList _bindingList; 

    bool IListSource.ContainsListCollection { get { return false; } } 

    IList IListSource.GetList() 
    { 
        return _bindingList ?? (_bindingList = this.ToBindingList()); 
    } 
} 

}

After that open yourModel.tt file

  • Find and replace the two occurrences of “ICollection” with “ObservableListSource” (lines 296 and 484).
  • Find and replace the first occurrence of “HashSet” with “ObservableListSource” ( Line 50).
  • Do not replace the second occurrence of HashSet found later in the code.
Carlo
  • 151
  • 1
  • 3
  • 16