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.
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
Krasimir,
That was exactly what I needed. Thanks.
Now that I am adding the row, I want to highlight that row and I am setting the ActiveDataItem of the grid with the e.Patient object. The row will be highlighted for that object, but, the row that was highlighted before stays highlighted. How do I get rid of that? The grid is set to single selection.
Thanks.