1. XRTableCell常见属性
XRTableCell xrTableCell = new XRTableCell();
A. 字体及字体大小
xrTableCell.Font = new System.Drawing.Font("Arial", FontSize);
B. 内容是否可以换行
xrTableCell.WordWrap = False;
C. 文本对齐方式
xrTableCell.TextAlignment=DevExpress.XtraPrinting.TextAlignment.MiddleLeft;
D. 边框是否显示及边框宽度
xrTableCell.Borders = DevExpress.XtraPrinting.BorderSide.All;
xrTableCell.BorderWidth = 0.5F;
E. 宽度和高度
xrTableCell.WidthF = 30;
xrTableCell.HeightF = 30;
F. XRTableCell文本前景色
xrTableCell.ForeColor=Color.Red;
G. XRTableCell文本内容左右间隔;
DevExpress.XtraPrinting.PaddingInfo padRight= new DevExpress.XtraPrinting.PaddingInfo();
padRight.Right = 4;
XRTableCell xrTableCell = new XRTableCell();
xrTableCell.Padding = padRight;
2. xrTable合并单元格(主要针对旧版本)
在新做的一个页面要用到xrTable合并单元格。我先在Design界面加了一个GroupHeader,随后又在GroupHeader中放了一个XRPanel,最后在XRPanel放入由xrTable嵌套的实现合并XRTableCell的功能,最后导致每一页在切换到下一页时输入很奇怪的一条线,最后查了两三天才发现这个问题。后来经过确认其实不需要加XRPanel的,直接放入嵌套的xrTable就可以了,深刻的教训,值得深思!
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using DevExpress.XtraReports.UI; using System.Drawing.Printing; using System.Linq.Expressions; namespace DevExpressConsole { public partial class TrendAndReport : DevExpress.XtraReports.UI.XtraReport { public TrendAndReport() { InitializeComponent(); PrintDocument(); } private void PrintDocument() { float TotalWidth = this.PageWidth - this.Margins.Left - this.Margins.Right; float CellHeight = 30; float CondtWidth = 30; for (int i = 0; i <= 7; i++) { XRTable TableContainer = new XRTable(); TableContainer.CanGrow = true; TableContainer.BeginInit(); for(int j=0;j<8;j++) { XRTableRow TCRow = new XRTableRow(); TCRow.CanGrow = true; XRTableCell OrderNo = new XRTableCell(); OrderNo.CanGrow = true; OrderNo.WidthF = TotalWidth - CondtWidth - 400; OrderNo.HeightF = CellHeight; OrderNo.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; OrderNo.ProcessDuplicates = DevExpress.XtraReports.UI.ValueSuppressType.Suppress; OrderNo.Text = j + 1 + ""; OrderNo.WordWrap = true; TCRow.Cells.Add(OrderNo); XRTableCell Happy = new XRTableCell(); Happy.CanGrow = true; Happy.WidthF = 400; Happy.HeightF = CellHeight; Happy.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; Happy.Text = "I am very happy with this! "; Happy.WordWrap = true; TCRow.Cells.Add(Happy); XRTableCell HW = new XRTableCell(); HW.CanGrow = true; HW.WidthF = 200; HW.HeightF = CellHeight; HW.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; HW.Text = "Hello World! "; HW.WordWrap = true; TCRow.Cells.Add(HW); TableContainer.Rows.Add(TCRow); } TableContainer.KeepTogether = true; TableContainer.AdjustSize(); TableContainer.EndInit(); XRTable xrTab = new XRTable(); xrTab.CanGrow = true; xrTab.BeginInit(); xrTab.LocationF = new PointF(0, 25 + TableContainer.HeightF * i); XRTableRow row = new XRTableRow(); row.CanGrow = true; XRTableCell cell = new XRTableCell(); cell.CanGrow = true; cell.Borders = DevExpress.XtraPrinting.BorderSide.All; cell.Controls.Add(TableContainer); cell.WidthF = TableContainer.WidthF; row.Cells.Add(cell); XRTableCell CONT = new XRTableCell(); CONT.CanGrow = true; CONT.Text = "Cont'd"; CONT.WidthF = CondtWidth; CONT.HeightF = TableContainer.HeightF; CONT.Angle = 270; CONT.Borders = DevExpress.XtraPrinting.BorderSide.All; CONT.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; row.Cells.Add(CONT); GroupHeader1.Controls.Add(xrTab); xrTab.WidthF = TotalWidth; xrTab.HeightF = TableContainer.HeightF; xrTab.Rows.Add(row); xrTab.AdjustSize(); xrTab.EndInit(); } } } }
3. xrTable分页
其实xrTable只要为每个表设置了LocationF属性,就会被自动挤到下一页,为了需要,还是做了一个例子进行确认:
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using DevExpress.XtraReports.UI; using System.Drawing.Printing; using System.Linq.Expressions; namespace DevExpressConsole { public partial class TrendAndReport : DevExpress.XtraReports.UI.XtraReport { int k = 0; public TrendAndReport() { InitializeComponent(); while (k < 5) { PrintDocument(); } } public void PrintDocument() { int EveryTabeNum = 1; int TabPortaitSpan = 25; XRTable xt = new XRTable(); xt.CanGrow = true; xt.Borders = DevExpress.XtraPrinting.BorderSide.All; xt.BorderWidth = 0.5f; xt.BeginInit(); XRTableRow xrow_header = new XRTableRow(); XRTableCell xc_header = new XRTableCell(); xc_header.Text = "DevExpress Page Break Usage -- Table" + (k + 1).ToString(); xc_header.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; xrow_header.Cells.Add(xc_header); xt.Rows.Add(xrow_header); for (int i = 0; i < 6; i++) { XRTableRow xrow = new XRTableRow(); xrow.CanGrow = true; xrow.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; for (int j = 0; j < 3; j++) { XRTableCell xc = new XRTableCell(); xc.HeightF = 25f; if (i % 2 == 0) { if (i / 10 >= 1) { xc.Text = "ABCD"; } else { xc.Text = DateTime.Now.ToShortDateString(); } } else { xc.CanGrow = true; xc.Text = DateTime.Now.ToLocalTime().ToShortTimeString(); } xrow.Cells.Add(xc); } if (i % 2 == 0) { xrow.BackColor = Color.LightGray; } xt.Rows.Add(xrow); } xt.AdjustSize(); xt.EndInit(); xt.Font = new System.Drawing.Font(Font.FontFamily, 8f, FontStyle.Regular); //Setting the table position. xt.LocationF = new PointF(0, TabPortaitSpan * k + xt.HeightF * k); Detail.Controls.Add(xt); xt.WidthF = this.PageWidth - this.Margins.Left - this.Margins.Right-50; //Add page break for every EveryTabeNum tables. if ((k + 1) % EveryTabeNum == 0) { XRPageBreak pb = new XRPageBreak(); pb.LocationF = new PointF(0, TabPortaitSpan * k + xt.HeightF * (k + 1)); Detail.Controls.Add(pb); } k++; } } }
4. 动态改变xrTable的元素大小
xrTable元素的大小不是我们想的只要设置宽度和高度后会自动生效,它需要在xrTable.BeginInit()和TableContainer.AdjustSize();TableContainer.EndInit();的包裹之内执行,同时设置元素的属性CanGrow/CanShrink为True:
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using DevExpress.XtraReports.UI; using System.Drawing.Printing; using System.Linq.Expressions; namespace DevExpressConsole { public partial class TrendAndReport : DevExpress.XtraReports.UI.XtraReport { int k = 0; public TrendAndReport() { InitializeComponent(); PrintDocument(); } public void PrintDocument() { int TabPortaitSpan = 25; XRTable xt = new XRTable(); xt.CanGrow = true; xt.Borders = DevExpress.XtraPrinting.BorderSide.All; xt.BorderWidth = 0.5f; xt.BeginInit(); for (int i = 0; i < 4; i++) { XRTableRow xrow = new XRTableRow(); xrow.CanGrow = true; xrow.HeightF = 50f; xrow.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; for (int j = 0; j < 3; j++) { XRTableCell xc = new XRTableCell(); if (i % 2 == 0) { if (i / 10 >= 1) { xc.Text = "ABCD"; } else { xc.Text = DateTime.Now.ToShortDateString(); } } else { xc.Text = DateTime.Now.ToLocalTime().ToShortTimeString(); } xrow.Cells.Add(xc); } if (i % 2 == 0) { xrow.BackColor = Color.LightGray; } xt.Rows.Add(xrow); } xt.AdjustSize(); xt.EndInit(); xt.Font = new System.Drawing.Font(Font.FontFamily, 8f, FontStyle.Regular); xt.LocationF = new PointF(0, TabPortaitSpan * k + xt.HeightF * k); Detail.Controls.Add(xt); xt.WidthF = this.PageWidth - this.Margins.Left - this.Margins.Right-50; } } }
以下为设置前后的对比图:
设置前:
设置后:
5. 在XRTableCell.AdjustSize();XRTableCell.EndInit();前后设置XRTableCell宽度Width的区别
在XRTableCell.AdjustSize();XRTableCell.EndInit();执行前设置XRTable的宽度,则XRTableCell的宽度会以当前设置的值为准,不会再发生变化,如下图所示:
在XRTableCell.AdjustSize();XRTableCell.EndInit();执行后设置XRTable的宽度,则XRTableCell的宽度会以当前XRTable的宽度为基准根据内容自动调整宽度,如下所示:
6. 需要注意的几点
A. XRTable背景色可以通过整行去添加背景色,而不需要一个一个XRTableCell去设置:
XRTableRow xrTableRow = new XRTableRow();
xrTableRow.BackColor = Color.LightGray;
B. 设置XRTable的边框及颜色会覆盖xrTableRow和xrTableCell之前的设置,所以在设置时要从外到里进行设置。
7. 总结:
以上就是这次用递归动态生成XRTable时所领会到的经验和教训,特此总结,以便备用!