/// <summary> /// Binds an object's properties to <see cref="Control"/>s with the same ID as the propery name. 将对象的属性值表现到控件上. /// </summary> /// <param name="obj">The object whose properties are being bound to forms Controls</param> /// <param name="container">The control in which the form Controls reside (usually a Page or ContainerControl)</param> public static void BindObjectToTableRow(CollectionBase cb, HtmlTable tb, string fileName) { if (cb == null) { return; } // Get the properties of the business object // //获取列的信息 PreViewColumnCollection pcc = (new PageBase()).GetColumns(fileName); //生成表头 HtmlTableRow th = new HtmlTableRow(); th.BgColor = "#ffffff"; foreach (PreViewColumn column in pcc) { HtmlTableCell tc = new HtmlTableCell(); tc.Width = column.Width.ToString(); tc.Align = "center"; tc.InnerHtml = column.Caption; tc.NoWrap = true; th.Cells.Add(tc); } tb.Rows.Add(th); //生成表的内容 foreach (object obj in cb) { HtmlTableRow tr = new HtmlTableRow(); tr.BgColor = "#ffffff"; Type objType = obj.GetType(); PropertyInfo[] objPropertiesArray = objType.GetProperties(); foreach (PreViewColumn column in pcc) { foreach (PropertyInfo objProperty in objPropertiesArray) { if (objProperty.Name == column.ID) { string propertyValue = objProperty.GetValue(obj, null).ToString(); if (column.Fomat == "num") { propertyValue = (double.Parse(propertyValue)).ToString("n2"); } HtmlTableCell tc = new HtmlTableCell(); tc.InnerText = propertyValue; tc.Align = column.Align; tc.Width = column.Width.ToString(); tr.Cells.Add(tc); } } tb.Rows.Add(tr); } } }
public PreViewColumnCollection GetColumns(string fileName) { fileName = string.Format("~/{0}/{1}.xml", Functions.GetAppConfigString("ConfigFilePath", "config"), fileName); fileName = Server.MapPath(fileName); XmlTextReader reader = new XmlTextReader(fileName); // 将reader移动到Where标签 if (reader.MoveToContent() == XmlNodeType.Element && reader.LocalName.Trim().Equals("table")) { PreViewColumnCollection collection = new PreViewColumnCollection(); while (reader.Read()) { if (reader.NodeType != XmlNodeType.Element) { continue; } PreViewColumn column = new PreViewColumn(); switch (reader.LocalName.Trim()) { case "td": column.ID = reader.GetAttribute("id"); if (reader.GetAttribute("width") != null && reader.GetAttribute("width") != "") { column.Width = int.Parse(reader.GetAttribute("width")); } if (reader.GetAttribute("unit") != null && reader.GetAttribute("unit") != "") { column.Unit = reader.GetAttribute("unit"); } if (reader.GetAttribute("align") != null && reader.GetAttribute("align") != "") { column.Align = reader.GetAttribute("align"); } if (reader.GetAttribute("fomat") != null && reader.GetAttribute("fomat") != "") { column.Fomat = reader.GetAttribute("fomat"); } column.Caption = reader.GetAttribute("caption"); break; } if (column.ID != null && column.ID != "") { collection.Add(column); } } reader.Close(); return(collection); } else { if (reader != null) { reader.Close(); } throw new ApplicationException(string.Format("配置文件({0})格式不正确!", fileName)); } }
public static void BindFieldsToTableRow(CollectionBase cb, Table tb, string fileName) { if (cb == null) { return; } // Get the properties of the business object // //获取列的信息 PreViewColumnCollection pcc = (new PageBase()).GetColumns(fileName); //生成表头 TableRow th = new TableRow(); th.CssClass = "tr_item"; foreach (PreViewColumn column in pcc) { TableCell tc = new TableCell(); if ((object)column.Width != null) { if (column.Unit == "%") { tc.Width = Unit.Percentage((double)column.Width); } else { tc.Width = Unit.Pixel(column.Width); } } tc.HorizontalAlign = HorizontalAlign.Center; tc.Text = column.Caption; tc.Wrap = false; tc.BorderWidth = Unit.Pixel(1); th.Cells.Add(tc); } tb.Rows.Add(th); //生成表的内容 foreach (object obj in cb) { TableRow tr = new TableRow(); tr.CssClass = "tr_item"; Type objType = obj.GetType(); FieldInfo[] objPropertiesArray = objType.GetFields(); foreach (PreViewColumn column in pcc) { foreach (FieldInfo objProperty in objPropertiesArray) { if (objProperty.Name == column.ID) { string propertyValue = objProperty.GetValue(obj).ToString(); if (column.Fomat == "num") { propertyValue = (double.Parse(propertyValue)).ToString("F"); } TableCell tc = new TableCell(); tc.Text = propertyValue; if (column.Align != null && column.Align != "") { switch (column.Align) { case "left": tc.HorizontalAlign = HorizontalAlign.Left; break; case "center": tc.HorizontalAlign = HorizontalAlign.Center; break; case "right": tc.HorizontalAlign = HorizontalAlign.Right; break; } } if ((object)column.Width != null) { if (column.Unit == "%") { tc.Width = Unit.Percentage((double)column.Width); } else { tc.Width = Unit.Pixel(column.Width); } } tc.BorderWidth = Unit.Pixel(1); tr.Cells.Add(tc); } } tb.Rows.Add(tr); } } }