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,
I am just checking if you require any further assistance on the matter.
Sincerely,
Krasimir
Developer Support Engineer
Infragistics
www.infragistics.com/support
Hello Mike,
I am very glad that the approach I have suggested to you was helpful.
I have been reading through the other question that you are having and I am not completely sure what is the scenario that you are having in which the GetRecordFromDataItem is always returning null. This method is reaching for a match of the object passed as parameter and the value of the DataItem property at the DataRecords in the XamDataGrid, if match is found a DataRecord object is returned and that record corresponds to the object passed as parameter to the method, if not, the method returns null. You can read more information regarding this method at the following articles in our online documentation: http://help.infragistics.com/NetAdvantage/WPF/2011.1/CLR4.0/?page=InfragisticsWPF4.DataPresenter.v11.1~Infragistics.Windows.DataPresenter.DataPresenterBase~GetRecordFromDataItem.html , http://help.infragistics.com/NetAdvantage/WPF/2011.1/CLR4.0/?page=xamDataPresenter_About_Data_Items_and_Data_Records.html . I have also created a sample application for you that demonstrates how the GetRecordFromDataItem method can be used with the BringRecordIntoView in order to scroll the XamDataGrid to a record based on an item form the DataSource of the XamDataGrid.
Please let me know if the sample application follows your scenario or I have misunderstood you in any way.
Krasimir,
Perfect, thank you, it was just what I needed.
Did you guys ever disuss the larger issue of why GetRecordFromDataItem would always return null except after adding a record? I had to write my own find method. Any thoughts at all?
Thanks again Krasimir for coming through for me yet again.
I have been looking into the behavior that you have described and when you click on a record it becomes Active and Selected. After adding the new Patient to the DataSource of the XamDataGrid and set the ActiveDataItem to the newly added item, the Record that correspond to that Patient becomes active. The record that was previously active is no longer active, but it stays selected. In that case, the previous active record is selected and the newly activated record is active and both records appear highlighted. You can force the selected record to not appear highlighted by clearing the selected record before setting the ActiveDataItem property of the XamDataGrid. Here is an example for that:
(dgPatients.DataSource as ObservableCollection<Patients>).Add(e.Patient);
dgPatients.SelectedItem.Records.Clear();
dgPatients.ActiveDataItem = e.Patient;
If you have any further questions please do not hesitate to ask.
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.