Hello,
I've got the following scenario: I'm Binding a BindingList of objects to the XAM DataGrid. The User can add new objects via the AddNew Row to the Grid.
All Objects of the List have some common string properties ... and a special ID property which is a combination of the before mentioned string properties.
When I add a new record to the grid, this ID property should be set automatically.( In the added Record and in the Underlying Dataobject.)
I experimented with the RecordUpdating and the RecordUpdated events ... But the records of this events are only readonly.
When I check the Events RecordAdding and RecordAdded a new object in the List will be created but the properties aren't set yet.
-> Is there a possibillity to add a new object / record with a automatically generated CellValue which is a combination of the other record cell Values?
I'm sorry if this sounds a bit complicated... I couldn't describe it in a better way. Any help would be highly appriciated.
Thanks
Thanks for the reply and the clarification of the events.
Everything is much clearer now!
Your solution is working fine.
Just to clarify on these three events.
When you start typing into cell of the AddNewRecord for the first time, a new object of your underlying type is created - the parameterless constructor is called. If you set some properties inside this constructor, they will be filled inside the cells as initial values. In the record adding event, you can either mark the record as add record (as you have done with the Tag property) or use its IsAddRecord property to check for the same. The RecordUpdating event is a cancellable event that is fired when you press Enter or the record leaves focus and the record tries to update. RecordUpdated is fired when the record is committed into the XamDataGrid and the changes are accepted.
Regarding your Id property, what you could do is use the same logic in the getter of the Id property and get the values of the other properties. If you have implemented INotifyPropertyChanged, the Id will be updated when the other change.
Got a solution ... seems to work, but I really don't know:
private void xamgrid_RecordAdded( ... ) { e.Record.Tag = "IsAddNew"; }
private void xamgrid_RecordUpdating(...) { if ((string)e.Record.Tag == "IsAddNew") {
Object object = e.Record.DataItem as Object; if (!object.CheckKeyAttributes()) { MessageBox.Show("All Attributes have to be set!"); e.Record.CancelUpdate(); } } }
private void xamgrid_RecordUpdated( ... ) { if ((string) e.Record.Tag == "IsAddNew") { Object object = e.Record.DataItem as Object;
// Setting the ID Field
string ID = object.Prop1 + "_" + object.Prop2 + "_" + object.Prop3 + "_" + objec.Prop4;
e.Record.SetCellValue(Field_ID, ID);
e.Record.Tag = null;
}