Binding DataGridView to ObservableCollection<T>

3 posts, 1 answers
  1. Alex
    Alex avatar
    7 posts
    Member since:
    Mar 2013

    Posted 08 May 2013 Link to this post

    Hello!

    I am working in VS2012, WindowsForms application, TargetFwk = 4.5 and trying to bind a DataGridView control to a collection that allows the grid to automatically handle add and remove from the collection. I tried ObservableCollection<T> as it implements INotifyCollectionChanged and seems a good candidate to what I am trying to achieve.

    I have a Document which has DocumentDetails, so the grid binds to the details. What I've done:

    1) created a copy of the generation templates within the solution and customized collection generation (ObservableCollection<T> instead of IList<T>) as indicated by Telerik here 

    After customization details property looks like this (within Document entity - generated code):

    private ObservableCollection<DocumentDetail> _documentDetails = new ObservableCollection<DocumentDetail>();
    [Collection(InverseProperty = "Document")]
    [Storage("_documentDetails")]
    public virtual ObservableCollection<DocumentDetail> DocumentDetails 

       get
       {
           return this._documentDetails;
       }
    }

    instead of the classical one

    private IList<DocumentDetail> _documentDetails = new IList<DocumentDetail>();
    [Collection(InverseProperty = "Document")]
    [Storage("_documentDetails")]
    public virtual IList<DocumentDetail> DocumentDetails 

       get
       {
           return this._documentDetails;
       }
    }


    2) The solution loads and existing document having several details by:

    i) load: theDoc = context.Documents.Where(
    d => d.DocumentId == documentId).FirstOrDefault();

    ii) bind: dgDocumentDetails.DataSource = theDoc.DocumentDetails;

    Before changing to ObservableCollection<DocumentDetail> i) loads data from Document and ii) loads the details (lazy loading)
    After the change i) tries to load everything, but fails because it tries to get data from an nonexistent table:

    declare @p1 int
    set @p1=-1
    exec sp_prepexec @p1 output,N'@p0 int',N'SELECT a.[DocumentId] AS COL1, b.[DocumentId] AS COL2, c.[DocumentDetailId] AS COL3, c.[Amount] AS COL4, c.[DocumentId] AS COL5, c.[DocumentId] AS COL6, c.[TotalAmountWithVAT] AS COL7, c.[VATAmount] AS COL8, b.[seq] AS xj2 FROM [Document] a JOIN [Document_DocumentDetail] AS b ON (a.[DocumentId] = b.[DocumentId]) LEFT JOIN [DocumentDetail] AS c ON (b.[DocumentDetailId] = c.[DocumentDetailId]) WHERE a.[DocumentId] = @p0                                       ORDER BY COL2, xj2 ',@p0=1
    select @p1

    Any idea how can I make this work?

    Thank you,

    Alex D.
  2. Answer
    Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 13 May 2013 Link to this post

    Hi Alex,

    Thank you for reporting that behaviour in details. Please kindly find your Telerik points increased by 500.

    Generally, Telerik OpenAccess ORM does not support the usage of custom collections for the navigation properties except our TrackedBindingList<T> and TrackedList<T> types. Additionally, the described behaviour with the Attributes mapping types is a bug on our side that is placed in our TODO list and it will be fixed for one of the future releases.

    Regarding the scenario you implement, we defined a workaround that will help you with that task. It involves the usage of our TrackedBindingList<T> class instead of ObservableCollection<T> and the XML mapping type. The change in the types is necessary in order for the OpenAccess context to track the changes in the collections and with the XML mapping type you will avoid the generation of the incorrect SQL statement.

    TrackedBindingList<T> can be found in the Telerik.OpenAccess namespace and is based on BindingList<T>. Basically, it helps us with our change tracking capabilities. The process for utilizing it would be to remove the unnecessary using statement from General.ttinclude and to replace ObservableCollection with TrackedBindingList in Specific.ttinclude.

    The change of the mapping type can be done with our Model Settings dialogue, the Code Generation tab, the Mapping Type drop-down.

    Moreover, the documentation article you refer to will be updated accordingly, so that it does not contain any misleading information.

    If you experience any difficulties wit the suggested solution or have additional question, do not hesitate to get back to us.


    All the best,
    Doroteya
    the Telerik team
    OpenAccess Samples Kit boasts 50+ sample applications providing diverse real-life business solutions. Click to read more and see OpenAccess ORM in action.
  3. DevCraft banner
  4. Alex
    Alex avatar
    7 posts
    Member since:
    Mar 2013

    Posted 23 May 2013 Link to this post

    Hello!

    I have changed the templates to use TrackedBindingList<T> and now, adding and removing items function correctly.

    Thanks,

    Alex D.
Back to Top