public static void AddSumRow <T>(List <T> list, string[] statColumns, bool[] abs4Column) where T : new() { T local = new T(); IStatisticabled statisticabled = local as IStatisticabled; if (statisticabled != null) { statisticabled.IsStatistics = true; } for (int i = 0; i < statColumns.Length; i++) { string propertyName = statColumns[i]; double val = 0.0; foreach (T local2 in list) { object property = ReflectionHelper.GetProperty(local2, propertyName); if (!(((abs4Column != null) && (abs4Column.Length != 0)) && abs4Column[i])) { val += double.Parse(property.ToString()); } else { val += Math.Abs(double.Parse(property.ToString())); } } object proValue = TypeHelper.ChangeType(typeof(T).GetProperty(propertyName).PropertyType, val); ReflectionHelper.SetProperty(local, propertyName, proValue); } list.Add(local); }
/// <summary> /// 绑定数据报表。并为其增加【小计】【总计】行。 /// </summary> /// <typeparam name="T">报表记录对象的类型</typeparam> /// <param name="dgw">要绑定的DataGridView</param> /// <param name="list">数据行对象列表。执行完成后,将在该列表的最后添加一个【小计】对象。</param> /// <param name="statColumns">需要进行统计的列</param> /// <param name="totalRow">在有分页的情况下,【总计】的行对象</param> /// <param name="abs4Column">对目标列进行统计时,是否取其绝对值进行统计</param> /// <param name="isDisplayCol">是否显示列</param> /// <param name="DistinguishSize">区分是否是尺码列</param> public static void BindSource4Reports <T>(DataGridViewPagingSumCtrl ctrl, DataGridView dgw, IList <T> orgList, string[] statColumns, T totalRow, bool[] abs4Column, bool isPaging, bool isDisplayCol = true, bool DistinguishSize = true) where T : class, new() { ctrl.ClearSummary(); ctrl.abs4Column = abs4Column; if (orgList == null || orgList.Count == 0) { orgList = null; ctrl.SetDataSource(orgList, isDisplayCol, DistinguishSize); return; } if (statColumns == null || statColumns.Length == 0) { ctrl.SetDataSource(orgList, isDisplayCol, DistinguishSize); return; } // List<T> list = new List<T>(); List <T> calList = new List <T>(); // list.AddRange(orgList); Type type = typeof(T); foreach (string column in statColumns) { PropertyInfo pro = type.GetProperty(column); if (pro == null) { throw new Exception(string.Format("Class [{0}] does not contain property [{1}] .", CJBasic.Helpers.TypeHelper.GetClassSimpleName(type), column)); } if (!pro.CanRead || !pro.CanWrite) { throw new Exception(string.Format("Property [{0}] of class [{1}] must be readable and writable.", column, CJBasic.Helpers.TypeHelper.GetClassSimpleName(type))); } } if (isPaging) { AddSumRow1(dgw, orgList, calList, statColumns, abs4Column); } if (totalRow != null) { IStatisticabled obj = totalRow as IStatisticabled; if (obj != null) { obj.IsStatistics = true; } calList.Add(totalRow); } if (dgw.RowHeadersWidth < 70) { dgw.RowHeadersWidth = 70; } ctrl.SetDataSource(orgList, isDisplayCol, DistinguishSize); ctrl.ShowSummary(dgw, calList, statColumns); }
public static void BindSource4Reports <T>(DataGridView dgw, List <T> list, string[] statColumns, T totalRow, bool[] abs4Column) where T : class, new() { if ((((list != null) && (list.Count != 0)) && (statColumns != null)) && (statColumns.Length != 0)) { System.Type t = typeof(T); foreach (string str in statColumns) { PropertyInfo property = t.GetProperty(str); if (property == null) { throw new Exception(string.Format("Class [{0}] does not contain property [{1}] .", TypeHelper.GetClassSimpleName(t), str)); } if (!(property.CanRead && property.CanWrite)) { throw new Exception(string.Format("Property [{0}] of class [{1}] must be readable and writable.", str, TypeHelper.GetClassSimpleName(t))); } } AddSumRow <T>(list, statColumns, abs4Column); if (totalRow != null) { IStatisticabled statisticabled = totalRow as IStatisticabled; if (statisticabled != null) { statisticabled.IsStatistics = true; } list.Add(totalRow); } if (dgw.RowHeadersWidth < 0x41) { dgw.RowHeadersWidth = 0x41; } dgw.DataSource = list; DataGridViewCellStyle style = new DataGridViewCellStyle(); style.Font = new Font(dgw.DefaultCellStyle.Font.Name, dgw.DefaultCellStyle.Font.Size, FontStyle.Bold); style.Alignment = DataGridViewContentAlignment.MiddleRight; dgw.RowHeadersDefaultCellStyle = style; dgw.Rows[list.Count - 1].HeaderCell.Value = "总计"; dgw.Rows[list.Count - 1].DefaultCellStyle = style; DataGridViewCellStyle emptyStyle = new DataGridViewCellStyle(); emptyStyle.ForeColor = Color.Transparent; emptyStyle.SelectionForeColor = Color.Transparent; SetCellContentEmpty <T>(dgw.Rows[list.Count - 1], statColumns, emptyStyle); if (totalRow != null) { dgw.Rows[list.Count - 2].HeaderCell.Value = "小计"; dgw.Rows[list.Count - 2].DefaultCellStyle = style; SetCellContentEmpty <T>(dgw.Rows[list.Count - 2], statColumns, emptyStyle); } } }
/// <summary> /// 为数据报表增加一个汇总行。 /// </summary> /// <typeparam name="T">报表记录对象的类型</typeparam> /// <param name="list">数据行对象列表。执行完成后,将在该列表的最后添加一个【小计】对象。</param> /// <param name="statColumns">需要进行统计的列</param> /// <param name="abs4Column">对目标列进行统计时,是否取其绝对值进行统计</param> /// <param name="isDisplayColumn">是否显示列</param> /// <param name="DistinguishSize">区分是否是尺码列</param> public static void AddSumRow1 <T>(DataGridView dgw, IList <T> list, IList <T> calList, string[] statColumns, bool[] abs4Column, bool isDisplayColumn = true, bool DistinguishSize = true) where T : new() { T sum = new T(); try { IStatisticabled obj = sum as IStatisticabled; if (obj != null) { obj.IsStatistics = true; } for (int i = 0; i < statColumns.Length; i++) //针对每一个汇总项 { string column = statColumns[i]; decimal total = 0; if (list != null) { foreach (T t in list) //统计 { object val = ReflectionHelper.GetProperty(t, column); if (abs4Column == null || abs4Column.Length == 0 || !abs4Column[i]) { total += decimal.Parse(val.ToString()); } else { total += Math.Abs(decimal.Parse(val.ToString())); } } } //648 销售单查询:总计中销售额出现计算错误。 total = Math.Round(total, 2, MidpointRounding.AwayFromZero); Type type = typeof(T); PropertyInfo info = type.GetProperty(column); object newTotal = TypeHelper.ChangeType(info.PropertyType, total); ReflectionHelper.SetProperty(sum, column, newTotal); foreach (DataGridViewColumn item in dgw.Columns) { if (item.DataPropertyName == column) { if (DistinguishSize == false) { if (total == 0 && isDisplayColumn == false) { item.Visible = false; break; } else { item.Visible = true; break; } } } } } } catch (Exception ex) { CommonGlobalUtil.WriteLog(ex); } calList.Add(sum); }