public virtual string FormatPropertyColumnValue(IMGridPropertyColumn pColumn, IMPropertyInfo pPropertyInfo, T pRow) { object value = pPropertyInfo.GetValue(pRow); if (value == null) { return(null); } if (pColumn.StringFormat != null) { return(string.Format(pColumn.StringFormat, value)); } if (pPropertyInfo.PropertyType == null) { throw new ArgumentException($"{nameof(pPropertyInfo.PropertyType)} of column {pColumn.Identifier} is null, please specify it"); } Type pType = Nullable.GetUnderlyingType(pPropertyInfo.PropertyType) ?? pPropertyInfo.PropertyType; if (pType == typeof(bool)) { return((bool)value ? L["True"] : L["False"]); } if (pType.IsEnum) { return(((Enum)value).ToName()); } if (pType == typeof(DateTime)) { if (HasAttribute(pColumn, pPropertyInfo, typeof(TimeAttribute))) { return(string.Format("{0:t}", ((DateTime)value))); } if (HasAttribute(pColumn, pPropertyInfo, typeof(DateTimeAttribute))) { return(string.Format("{0:g}", ((DateTime)value))); } return(string.Format("{0:d}", ((DateTime)value))); } return(value.ToString()); }
public virtual string FormatPropertyColumnValue(IMGridPropertyColumn pColumn, IMPropertyInfo pPropertyInfo, T pRow) { object value = pPropertyInfo.GetValue(pRow); if (value == null) { return(null); } if (pColumn.StringFormat != null) { return(String.Format(pColumn.StringFormat, value)); } Type pType = Nullable.GetUnderlyingType(pPropertyInfo.PropertyType) ?? pPropertyInfo.PropertyType; if (pType == typeof(bool)) { return((bool)value ? L["True"] : L["False"]); } if (pType.IsEnum) { return(((Enum)value).ToName()); } if (pType == typeof(DateTime)) //TODO { if (HasAttribute(pColumn, pPropertyInfo, typeof(TimeAttribute))) { return(((DateTime)value).ToString("HH:mm")); } if (HasAttribute(pColumn, pPropertyInfo, typeof(DateTimeAttribute))) { return(((DateTime)value).ToString("yyyy-MM-ddTHH:mm")); } return(((DateTime)value).ToString("yyyy-MM-dd")); } return(value.ToString()); }
protected bool HasAttribute(IMGridPropertyColumn pColumn, IMPropertyInfo pPropertyInfo, Type pType) { return(pPropertyInfo.GetCustomAttribute(pType) != null || (pColumn.Attributes != null && pColumn.Attributes.Any(a => a.GetType() == pType))); }
private static Cell GetPropertyColumnCell <T>(IMGridObjectFormatter <T> pFormatter, T rowData, IMGridPropertyColumn popcolumn, IMPropertyInfo iprop) { Cell cell; if (iprop.PropertyType == typeof(DateTime) || iprop.PropertyType == typeof(DateTime?)) { var datetime = iprop.GetValue(rowData) as DateTime?; cell = CreateDateCell(datetime); } else if (iprop.PropertyType == typeof(int) || iprop.PropertyType == typeof(int?)) { var value = iprop.GetValue(rowData) as int?; string strvalue = value.HasValue ? value.Value.ToString(CultureInfo.InvariantCulture) : string.Empty; cell = CreateNumberCell(strvalue); } else if (iprop.PropertyType == typeof(long) || iprop.PropertyType == typeof(long?)) { var value = iprop.GetValue(rowData) as long?; string strvalue = value.HasValue ? value.Value.ToString(CultureInfo.InvariantCulture) : string.Empty; cell = CreateNumberCell(strvalue); } else if (iprop.PropertyType == typeof(float) || iprop.PropertyType == typeof(float?)) { var value = iprop.GetValue(rowData) as float?; string strvalue = value.HasValue ? value.Value.ToString(CultureInfo.InvariantCulture) : string.Empty; cell = CreateNumberCell(strvalue); } else if (iprop.PropertyType == typeof(double) || iprop.PropertyType == typeof(double?)) { var value = iprop.GetValue(rowData) as double?; string strvalue = value.HasValue ? value.Value.ToString(CultureInfo.InvariantCulture) : string.Empty; cell = CreateNumberCell(strvalue); } else { string cellValue = pFormatter.FormatPropertyColumnValue(popcolumn, iprop, rowData); cell = CreateTextCell(cellValue ?? string.Empty); } return(cell); }
private void AddPropertyField <TProperty>(RenderTreeBuilder pBuilder, IMGridColumn pColumn, IMGridPropertyColumn pPropertyColumn, bool pIsInFilterRow, double pLeftOffset, BoundingBox pBoundingBox) { var attributes = mPropertyInfoCache[pPropertyColumn].GetAttributes()?.ToList() ?? new List <Attribute>(); if (pIsInFilterRow) { if (!pColumn.EnableFilter && !attributes.OfType <ReadOnlyAttribute>().Any()) { attributes.Add(new ReadOnlyAttribute()); } if (pColumn.EnableFilter && attributes.Any(a => a is ReadOnlyAttribute)) { attributes = attributes.Where(a => !(a is ReadOnlyAttribute)).ToList(); } } if (pColumn is IMGridComplexEditableColumn <TProperty> complex) { if (pIsInFilterRow) { pBuilder.OpenComponent <MComplexPropertyField <ExpandoObject, TProperty> >(5); } else { pBuilder.OpenComponent <MComplexPropertyField <T, TProperty> >(5); } pBuilder.AddAttribute(776, "Property", pPropertyColumn.Property); pBuilder.AddAttribute(777, "PropertyType", typeof(TProperty)); pBuilder.AddAttribute(778, "Attributes", attributes.ToArray()); if (complex.FormTemplate != null && !pIsInFilterRow || (pIsInFilterRow && pColumn.EnableFilter)) { pBuilder.AddAttribute(781, "Template", complex.FormTemplate); } pBuilder.AddStyleWithAttribute(784, Extensions.MFORM_IN_TABLE_ROW_TD_STYLE_ATTRIBUTE, pLeftOffset, pBoundingBox); pBuilder.CloseComponent(); } else { pBuilder.OpenComponent <MField>(790); pBuilder.AddAttribute(792, "Property", pPropertyColumn.Property); pBuilder.AddAttribute(793, "PropertyType", typeof(TProperty)); pBuilder.AddAttribute(794, "Attributes", attributes.ToArray()); pBuilder.AddStyleWithAttribute(976, Extensions.MFORM_IN_TABLE_ROW_TD_STYLE_ATTRIBUTE, pLeftOffset, pBoundingBox); pBuilder.CloseComponent(); } }
protected bool HasAttribute <AT>(IMGridPropertyColumn pColumn, IMPropertyInfo pPropertyInfo) where AT : Attribute { return(pPropertyInfo.GetCustomAttribute <AT>() != null || (pColumn.Attributes != null && pColumn.Attributes.Any(a => a.GetType() == typeof(AT)))); }