This is a migrated thread and some comments may be shown as answers.

Binding DataGridView to ObservableCollection<T>

2 Answers 107 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Alex
Top achievements
Rank 1
Alex asked on 08 May 2013, 03:10 PM
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 Answers, 1 is accepted

Sort by
0
Accepted
Doroteya
Telerik team
answered on 13 May 2013, 02:53 PM
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.
0
Alex
Top achievements
Rank 1
answered on 23 May 2013, 05:19 PM
Hello!

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

Thanks,

Alex D.
Tags
Data Access Free Edition
Asked by
Alex
Top achievements
Rank 1
Answers by
Doroteya
Telerik team
Alex
Top achievements
Rank 1
Share this question
or