public DateTimeModelBinder( DateTimeFormatAttribute dateTimeFormatAttribute, DateTimeStyles supportedStyles) { this.dateTimeFormatAttribute = dateTimeFormatAttribute; this.supportedStyles = supportedStyles; }
public void ConstructorTest() { // Arrange const string Value = "value"; // Act var attr = new DateTimeFormatAttribute(Value); // Assert Assert.AreEqual(Value, attr.Value); }
/// <summary> /// Gets a date input property as a string. /// </summary> /// <param name="propertyInfo"></param> /// <param name="dateTimeFormatAttribute"></param> /// <param name="propValue"></param> /// <returns></returns> private (bool isDate, string dateTimeVal) GetDateStringValue(PropertyInfo propertyInfo, DateTimeFormatAttribute dateTimeFormatAttribute, object propValue) { var dateVal = ""; var isDateTimeProperty = propertyInfo.PropertyType == typeof(DateTime) || propertyInfo.PropertyType == typeof(DateTime?); var hasDateFormat = DefaultDateFormat != null || dateTimeFormatAttribute != null; var isDate = isDateTimeProperty && hasDateFormat; if (isDate) { dateVal = ((DateTime)propValue).ToString(dateTimeFormatAttribute?.DateTimeFormat ?? DefaultDateFormat); } return(isDate, dateVal); }
private string SetPropertyValue <TData>(PropertyInfo p, DateTimeFormatAttribute dateFormat, object propValue, int width) { string value; var dateVal = GetDateStringValue(p, dateFormat, propValue); if (dateVal.isDate) { value = dateVal.dateTimeVal; } else { value = propValue.ToString(); value = width < value.Length ? value.Substring(0, width) : value; //truncate field when value is larger then the width attribute. } return(value); }
private static bool TryGetDateTimeFormatAttribute( ModelBinderProviderContext context, out DateTimeFormatAttribute attr) { attr = null; if (context.Metadata is DefaultModelMetadata md) { attr = md.Attributes.Attributes .Where(a => a is DateTimeFormatAttribute) .Cast <DateTimeFormatAttribute>() .SingleOrDefault(); if (attr != null) { return(true); } } return(false); }
/// <summary> /// Init Worksheet /// </summary> private void InitWorksheet() { WorksheetAttribute classAttribue = _RecordModelType.GetCustomAttribute(typeof(WorksheetAttribute)) as WorksheetAttribute; if (classAttribue != null) { _Worksheet.Name = classAttribue.Title; } else { _Worksheet.Name = _RecordModelType.Name; } PropertyInfo[] properties = _RecordModelType.GetProperties(); Dictionary <PropertyInfo, ColumnHeaderAttribute> ColHeaderAttributeDict = new Dictionary <PropertyInfo, ColumnHeaderAttribute>(); foreach (PropertyInfo property in properties) { ColumnHeaderAttribute headerAttribute = property.GetCustomAttribute(typeof(ColumnHeaderAttribute)) as ColumnHeaderAttribute; if (headerAttribute != null && headerAttribute.IsVisible) //filter invisible item { ColHeaderAttributeDict.Add(property, headerAttribute); } } if (ColHeaderAttributeDict.Count < 1) { #if DEBUG Console.WriteLine("InitWorksheet Failed: HeaderAttributes.Count is 0."); #endif return; } ColHeaderAttributeDict = ColHeaderAttributeDict.OrderBy(one => one.Value.Index).ToDictionary(one => one.Key, one => one.Value); // order by index // Re-Set Index for (int i = 0; i < ColHeaderAttributeDict.Keys.Count; i++) { var key = ColHeaderAttributeDict.Keys.ElementAt(i); ColHeaderAttributeDict[key].Index = i; } _Worksheet.Columns = ColHeaderAttributeDict.Count; RangePosition rangePosition = new RangePosition(); rangePosition.Cols = 1; rangePosition.Row = 0; rangePosition.Rows = _Worksheet.RowCount; for (int i = 0; i < properties.Count(); i++) { PropertyInfo property = properties[i]; var attribute = property.GetCustomAttribute(typeof(FormatAttributeBase)); if (attribute == null) { continue; } IFormatArgs formatArgs = attribute as IFormatArgs; if (formatArgs != null) { ColumnHeaderAttribute headerAttribute = (from key in ColHeaderAttributeDict.Keys where key.Equals(property) select ColHeaderAttributeDict[property]).FirstOrDefault(); if (headerAttribute != null && headerAttribute.IsVisible) { rangePosition.Col = headerAttribute.Index; //not work correctly switch (formatArgs.CellDataFormatFlag) { case CellDataFormatFlag.General: break; case CellDataFormatFlag.Number: { NumberFormatAttribute numberFormatAttribute = formatArgs as NumberFormatAttribute; NumberDataFormatter.NumberFormatArgs numberFormatter = new NumberDataFormatter.NumberFormatArgs(); if (numberFormatAttribute.DecimalPlaces != short.MaxValue) { numberFormatter.DecimalPlaces = numberFormatAttribute.DecimalPlaces; } numberFormatter.NegativeStyle = numberFormatAttribute.NegativeStyle; numberFormatter.UseSeparator = numberFormatAttribute.UseSeparator; numberFormatter.CustomNegativePrefix = numberFormatAttribute.CustomNegativePrefix; numberFormatter.CustomNegativePostfix = numberFormatAttribute.CustomNegativePostfix; _Worksheet.SetRangeDataFormat(rangePosition, CellDataFormatFlag.Number, numberFormatter); //_ReoGridControl.DoAction(new SetRangeDataFormatAction(rangePosition, CellDataFormatFlag.Number, numberFormatter)); break; } case CellDataFormatFlag.DateTime: { DateTimeFormatAttribute dateTimeFormatAttribute = formatArgs as DateTimeFormatAttribute; DateTimeDataFormatter.DateTimeFormatArgs dateTimeFormatArgs = new DateTimeDataFormatter.DateTimeFormatArgs(); dateTimeFormatArgs.Format = dateTimeFormatAttribute.Format; dateTimeFormatArgs.CultureName = dateTimeFormatAttribute.CultureName; _Worksheet.SetRangeDataFormat(rangePosition, CellDataFormatFlag.DateTime, dateTimeFormatArgs); break; } case CellDataFormatFlag.Percent: break; case CellDataFormatFlag.Currency: break; case CellDataFormatFlag.Text: break; case CellDataFormatFlag.Custom: break; default: break; } } } } _ColumnWidthList = new List <int>(); _RowHeightList = new List <int>(); for (int i = 0; i < ColHeaderAttributeDict.Count; i++) { ColumnHeaderAttribute headerAttribute = ColHeaderAttributeDict.ElementAt(i).Value; if (string.IsNullOrEmpty(headerAttribute.Text)) { _Worksheet.ColumnHeaders[i].Text = ColHeaderAttributeDict.ElementAt(i).Key.Name; } else { _Worksheet.ColumnHeaders[i].Text = headerAttribute.Text; } if (headerAttribute.Width <= 0) { _Worksheet.ColumnHeaders[i].IsAutoWidth = true; } else { _Worksheet.ColumnHeaders[i].Width = (ushort)headerAttribute.Width; } _Worksheet.ColumnHeaders[i].IsVisible = true; _Worksheet.ColumnHeaders[i].Tag = ColHeaderAttributeDict.ElementAt(i).Key; // Tag stores PropertyInfo of Model _ColumnWidthList.Add(_Worksheet.ColumnHeaders[i].Width); // store column width } //stroe row height for (int i = 0; i < _Worksheet.Rows; i++) { _RowHeightList.Add(_Worksheet.RowHeaders[i].Height); } // load data LoadRecords(); _Worksheet.BeforeCellEdit += Worksheet_BeforeCellEdit; _Worksheet.CellDataChanged += Worksheet_CellDataChanged; _Worksheet.RangeDataChanged += Worksheet_RangeDataChanged; _Worksheet.AfterPaste += Worksheet_AfterPaste; _Worksheet.RowsHeightChanged += Worksheet_RowsHeightChanged; _Worksheet.ColumnsWidthChanged += Worksheet_ColumnsWidthChanged; }
public DateTimeConverter(DateTimeFormatAttribute dateTimeFormatAttribute) { this.dateTimeFormatAttribute = dateTimeFormatAttribute; }