private void OnInitializeRecord(object sender, InitializeRecordEventArgs e) { DataRecord dr = e.Record as DataRecord; if (dr != null) { Cell cell = dr.Cells["CustomerName"]; } }
A FieldLayout defines the Fields (columns) contained in a particular DataRecord. When a DataRecord is created, the FieldLayouts collection is searched for an existing FieldLayout whose Fields match the DataItem's properties. If a FieldLayout is found, it is associated with the DataRecord; if it is not found, a new FieldLayout is created. When a new FieldLayout is created, the FieldLayoutInitializing and FieldLayoutInitialized events are raised.
If the new FieldLayout’s AutoGenerateFieldsResolved property returns True, the Fields collection is automatically populated with a Field for every public property on the data item.
The auto-generation of Fields is done between the FieldLayoutInitializing and FieldLayoutInitialized events.
The automatic generation of Fields combined with the automatic generation and view-dependent arrangement of CellValuePresenters (described below), provides a great "no-touch" development solution for binding to complex data sources and displaying a robust UI for displaying/editing data, all with minimal developer effort.
The AssigningFieldLayoutToItem event is then raised to allow a different FieldLayout to be assigned to the DataRecord. Then, finally, the InitializeRecord event is raised.
The InitializeRecord event is raised for DataRecords, GroupByRecords and ExpandableFieldRecords so the InitializeRecordEventArgs exposes a property that returns the base Record class. Therefore, to access properties that are specific to one of the derived Record types, you need to cast to the appropriate type, as shown in the example code below.
private void OnInitializeRecord(object sender, InitializeRecordEventArgs e) { DataRecord dr = e.Record as DataRecord; if (dr != null) { Cell cell = dr.Cells["CustomerName"]; } }
The example XAML below shows how to manually add fields instead of using the AutoGenerateFields feature. It also shows how to add additional unbound fields:
<igDP:XamDataGrid> <igDP:XamDataGrid.FieldLayoutSettings> <igDP:FieldLayoutSettings AutoGenerateFields="False"/> </igDP:XamDataGrid.FieldLayoutSettings> <igDP:XamDataGrid.FieldLayouts> <igDP:FieldLayout> <igDP:FieldLayout.Fields> <igDP:Field Name="CustomerName" Label="Customer Name"/> <igDP:Field Name="Address"/> <igDP:Field Name="City"/> <igDP:Field Name="MyExtraDataColumn" BindingType="Unbound" DataType="{x:Type sys:Decimal}"/> </igDP:FieldLayout.Fields> </igDP:FieldLayout> </igDP:XamDataGrid.FieldLayouts> </igDP:XamDataGrid>