private void PopulateDataItemPropertiesDictionary( object dataItem )
{
var dataItemProperties = dataItem.GetType().GetProperties().Where( a => !a.GetGetMethod().IsVirtual ).ToArray();
this.DataItemPropertiesDictionary = new Dictionary<string, DataFieldInfo>();
// add MergeFields based on the associated ColumnHeaderText of each property of the dataitem (without spaces or special chars)
foreach ( var itemPropInfo in dataItemProperties )
{
var gridField = LiquidField.ParentGrid.Columns.OfType<BoundField>().FirstOrDefault( a => a.DataField == itemPropInfo.Name );
if ( gridField != null )
{
var mergeFieldName = gridField.HeaderText.Replace( " ", string.Empty ).RemoveSpecialCharacters();
// NOTE: since we are using the HeaderText as the mergeFieldName, and that might not be unique, just add the first one if there are duplicates
if ( !this.DataItemPropertiesDictionary.ContainsKey( mergeFieldName ) )
{
this.DataItemPropertiesDictionary.Add( mergeFieldName, new DataFieldInfo { PropertyInfo = itemPropInfo, GridField = gridField } );
}
}
else
{
// add properties that aren't shown in the grid in the next loop
}
}
// add additional MergeFields for Properties of the dataitem that aren't already already a MergeField created from the ColumnHeaderText
foreach ( var itemPropInfo in dataItemProperties )
{
var mergeFieldName = itemPropInfo.Name;
if ( !this.DataItemPropertiesDictionary.ContainsKey( mergeFieldName ) )
{
this.DataItemPropertiesDictionary.Add( mergeFieldName, new DataFieldInfo { PropertyInfo = itemPropInfo, GridField = null } );
}
}
}