XtraReport交叉表自适应行高及最佳列宽
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XtraReport交叉表自适应行高及最佳列宽相关的知识,希望对你有一定的参考价值。
1.自适应行头的行高,绑定CustomRowHeight事件,代码如下:
private Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
private void pivotGrid1_CustomRowHeight(object sender, HuanSi.XtraReports.UI.PivotGrid.PivotCustomRowHeightEventArgs e) {
e.RowHeight = 20;
var grid=sender as HuanSi.XtraReports.UI.XRPivotGrid;
var list=e.Data.GetFieldsByArea(HuanSi.XtraPivotGrid.PivotArea.RowArea, false);
foreach (var item in list)
{
var obj=grid.GetFieldValue(item, e.RowIndex);
if (obj == null)
continue;
var font = (item as HuanSi.XtraReports.UI.PivotGrid.XRPivotGridField).Appearance.Cell.Font;
SizeF size = gr.MeasureString(obj.ToString(), font, item.Width);
int height = Convert.ToInt32(Math.Ceiling(size.Height));
e.RowHeight = e.RowHeight > height ? e.RowHeight : height;
}
}
效果图如下:
2.自适应数据的行高,同样绑定CustomRowHeight事件,代码如下:
private Graphics gr = Graphics.FromHwnd(IntPtr.Zero);
private void xrPivotGrid1_CustomRowHeight_1(object sender, PivotCustomRowHeightEventArgs e) {
e.RowHeight = 0;
for (int i = 0; i <= e.ColumnCount - 1; i++) {
string value = e.GetRowCellValue(i).ToString();
SizeF size = gr.MeasureString(value, e.DataField.Appearance.Cell.Font, e.DataField.Width);
int height = Convert.ToInt32(Math.Ceiling(size.Height));
e.RowHeight = e.RowHeight > height ? e.RowHeight : height;
}
}
盗用网上一张效果图:
3.自适应列宽
自适应列宽则极其简单,绑定BeforePrint事件即可,代码如下:
private void pivotGrid1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
var grid=sender as HuanSi.XtraReports.UI.XRPivotGrid;
grid.BestFit();
}
再次盗用DX官方的一张效果图:
以上是关于XtraReport交叉表自适应行高及最佳列宽的主要内容,如果未能解决你的问题,请参考以下文章
POI导出excel:设置字体颜色行高自适应列宽自适应锁住单元格合并单元格...