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;

}

}

效果图如下:

XtraReport交叉表自适应行高及最佳列宽

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;

}

}

盗用网上一张效果图:

XtraReport交叉表自适应行高及最佳列宽

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交叉表自适应行高及最佳列宽

上一篇:智能车学习(三)—— ADC学习


下一篇:Maven学习总结(一)——Maven入门