public void AddDataTableColumn(IConnectionHandler connectionHandler, object obj, FormStructure formStructure, ref DataTable table) { foreach (var property in obj.GetType().GetProperties()) { if (property.Name == "Dynamic" || property.PropertyType == typeof(ObjectState)) { continue; } var column = new DataColumn(); var propertyType = property.PropertyType; if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { propertyType = propertyType.GetGenericArguments()[0]; column.AllowDBNull = true; } column.DataType = propertyType; column.ColumnName = property.Name; if (!table.Columns.Contains(property.Name)) { table.Columns.Add(column); } } if (formStructure == null) { return; } new FormStructureBO().DeSerializeForm(ref formStructure); var generatorBo = new GeneratorBO(); formStructure.FormState = FormState.ReportMode; var generateForm = generatorBo.GenerateForm(connectionHandler, formStructure); if (generateForm == null) { return; } foreach (Control control in generateForm.Controls) { if (control == null || control.DisplayName == null) { continue; } if (!table.Columns.Contains(control.DisplayName)) { table.Columns.Add(control.DisplayName, control.DisplayValue != null ? control.DisplayValueType : typeof(string)); } } }
public void AddDataTableRows(IConnectionHandler connectionHandler, object obj, FormStructure formStructure, string objectName, string[] refcolumnNames, ref DataTable table) { var row = table.NewRow(); foreach (var property in obj.GetType().GetProperties()) { if (property.Name == "Dynamic" || property.PropertyType == typeof(ObjectState)) { continue; } var propertyType = property.PropertyType; if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable <>)) { propertyType = propertyType.GetGenericArguments()[0]; } if (property.GetValue(obj, null) != null) { row[property.Name] = System.Convert.ChangeType(property.GetValue(obj, null), propertyType); } } if (formStructure != null) { new FormStructureBO().DeSerializeForm(ref formStructure); if (refcolumnNames != null) { var str = ""; foreach (var refcolumnName in refcolumnNames) { var value = obj.GetType().GetProperty(refcolumnName).GetValue(obj, null); if (value == null) { continue; } if (!string.IsNullOrEmpty(str)) { str += ","; } str += value; } formStructure.RefId = str; } formStructure.ObjectName = objectName; formStructure.FormState = FormState.ReportMode; var generateForm = new GeneratorBO().GenerateForm(connectionHandler, formStructure); if (generateForm != null) { var stringWriter = new StringWriter(); var writer = new Html32TextWriter(stringWriter); foreach (var control in generateForm.Controls) { if (control == null) { continue; } var key = (Control)control; key.Writer = writer; if (key.DisplayName == null) { continue; } row[key.DisplayName] = key.DisplayValue != null?System.Convert.ChangeType(key.DisplayValue, key.DisplayValueType) : null; if (row[key.DisplayName] != null && generateForm.GetFormControl.Count > 0 && string.IsNullOrEmpty(row[key.DisplayName].ToString())) { key.Value = generateForm.GetFormControl.FirstOrDefault(c => c.Key == key.Id).Value; } key.Generate(); row[key.DisplayName] = key.DisplayValue; } } } table.Rows.Add(row); }
public DataTable ReportFormDataForExcel(IConnectionHandler connectionHandler, Guid formId, string culture) { try { var table = new DataTable(); var generatorBo = new GeneratorBO(); var byCulture = new FormStructureBO().Get(connectionHandler, formId); byCulture.FormState = FormState.ReportMode; var formStructure = generatorBo.GenerateForm(connectionHandler, byCulture); if (formStructure == null) { return(table); } byCulture = formStructure; foreach (Control control in byCulture.Controls) { if (control == null) { continue; } if (control.GetType() == typeof(Label) || control.GetType() == typeof(FileUploader)) { continue; } var columnName = control.GetCaption(); if (!table.Columns.Contains(columnName)) { table.Columns.Add(columnName, control.DisplayValue != null ? control.DisplayValueType : typeof(string)); } } if (culture == "fa-IR") { var ordinal = table.Columns.Count - 1; for (int i = 0; i < table.Columns.Count; i++) { table.Columns[0].SetOrdinal(ordinal); ordinal--; } } var @where = Where(connectionHandler, x => x.StructureId == formId); foreach (var formData in @where) { var row = table.NewRow(); var stringWriter = new StringWriter(); var writer = new Html32TextWriter(stringWriter); var list = Extentions.GetControlData(formData.Data); foreach (Control control in formStructure.Controls) { if (control.GetType() == typeof(Label) || control.GetType() == typeof(FileUploader)) { continue; } control.Writer = writer; control.FormState = FormState.DetailsMode; if (list != null) { control.Value = list.ContainsKey(control.Id) ? list[control.Id] : null; } control.Generate(); var columnName = control.GetCaption(); row[columnName] = control.DisplayValue != null?control.DisplayValue.ToString() : string.Empty; } table.Rows.Add(row); } return(table); } catch (KnownException ex) { Log.Save(ex.Message, LogType.ApplicationError, ex.Source, ex.StackTrace); throw new KnownException(ex.Message, ex); } catch (Exception ex) { Log.Save(ex.Message, LogType.ApplicationError, ex.Source, ex.StackTrace); throw new KnownException(ex.Message, ex); } }