Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
2275
Adding a row fails with ObservableCollection
posted

Hi, I'm trying to add a row to a XamDataGrid programmatically and I keep getting an exception with the error "Can't add items to a DataPresenter that is bound to a datasource."  My FieldLayoutSettings has AllowAddNew=True.  My call is 

dgPatients.DataItems.Add( e.Patient );
where dgPatients is the XamDataGrid, and e.Patient is a patient object.  The DataSource of the grid
was populated with an ObservableList.

Thank you.
  • 30945
    Offline posted

    Hello Mike,

     

    I have been reading into your post and it seems that the reason for the error that you are getting is that when bound to a source using the DataSource property, the XamDataGrid does not allow adding items through the DataItems property. In order to add new record you can add it directly to your ObservableCollection. For example you can use the following code line instead of dgPatients.DataItems.Add(e.Patient):

     

                    (dgPatients.DataSource as ObservableCollection<Patients>).Add(e.Patient);

     

    Since the ObservableCollection implements INotifyCollectionCanged interface the newly added item will be automatically added to the XamDataGrid as Record.

     

    If you need any further assistance on the matter please do not hesitate to ask.

     

    Sincerely,

    Krasimir

    Developer Support Engineer

    Infragistics

    www.infragistics.com/support

  • 2275
    posted

    Sorry, meant ObservableCollection, not ObservableList.  Could not edit post once I published.