public static List <GenericDataFieldAttribute> GetGenericDataFieldAttributes(GenericData obj = null, Type gtype = null, string name = null) { if (name != null) { var GTypes = Assembly.GetAssembly(typeof(GenericData)).GetTypes().Where(t => t.IsSubclassOf(typeof(GenericData))).ToList(); gtype = GTypes.First(x => x.Name == name); } var attrs = new List <GenericDataFieldAttribute>(); if (obj != null) { gtype = obj.GetType(); } if (gtype != null) { var props = gtype.GetProperties(); foreach (var pinfo in props) { var genericDataField = ((GenericDataFieldAttribute[])pinfo.GetCustomAttributes(typeof(GenericDataFieldAttribute), true)).FirstOrDefault(); if (genericDataField != null && !genericDataField.DisableGridEdit) { attrs.Add(genericDataField); } } } return(attrs); }
public object GetGenericItems() { if (EditedData.NotEmpty()) { var genericDataType = GTypes.First(x => x.Name == EditedData); GData = (DBController.DbGenericData.GetGenericData(new GenericDataSearchParameters { FromType = genericDataType }) as IList).ToDataTable(); var att = GenericData.GetGenericDataFieldAttributes(null, genericDataType, null); var propertyInfos = GData.Columns.OfType <DataColumn>().Select(x => x.ColumnName).ToList(); foreach (var propertyInfo in propertyInfos) { var idx = GData.Columns.IndexOf(propertyInfo); var genericFieldInfo = att.FirstOrDefault(x => x.PropertyName == propertyInfo); if (genericFieldInfo == null || genericFieldInfo.DisableGridEdit) { GData.Columns.RemoveAt(idx); } else { var rowsToFormat = GData.Rows.Count; var tmpObject = Activator.CreateInstance(genericDataType); for (int i = 0; i < rowsToFormat; i++) { var valueToFormat = GData.Rows[i][idx].ToString(); if (valueToFormat.NotEmpty()) { var formatedVal = (tmpObject as GenericData).GetFormatedValue(genericDataType, valueToFormat, genericFieldInfo, DBController); if (formatedVal.NotEmpty()) { GData.Rows[i][idx] = formatedVal; } } } GData.Columns[idx].ReadOnly = false; try { GData.Columns[idx].ColumnName = genericFieldInfo.FieldName; } catch (Exception) { } } } return(GData); } return(null); }