根据数据源字段动态设置报表中的列数量以及列宽度
原创
©著作权归作者所有:来自51CTO博客作者葡萄城技术团队的原创作品,请联系作者获取转载授权,否则将追究法律责任
在报表系统中,我们通常会有这样的需求,就是由用户来决定报表中需要显示的数据,比如数据源中共有八列数据,用户可以自己选择在报表中显示哪些列,并且能够自动调整列的宽度,已铺满整个页面。本文就讲解一下该功能的实现方法。
在报表系统中,我们通常会有这样的需求,就是由用户来决定报表中需要显示的数据,比如数据源中共有八列数据,用户可以自己选择在报表中显示哪些列,并且能够自动调整列的宽度,已铺满整个页面。本文就讲解一下ActiveReports中该功能的实现方法。
第一步:设计包含所有列的报表模板,将数据源中的所有列先放置到报表设计界面,并设置你需要的列宽,最终界面如下:
第二步:在报表的后台代码中添加一个Columns的属性,用于接收用户选择的列,同时,在报表的ReportStart事件中添加以下代码:
public List<string> Columns;
private void Report1_ReportStart(object
lines.Add(crossSectionLine1);
lines.Add(crossSectionLine2);
lines.Add(crossSectionLine3);
lines.Add(crossSectionLine4);
lines.Add(crossSectionLine5);
lines.Add(crossSectionLine6);
lines.Add(crossSectionLine7);
// 隐藏不需要显示的控件,并计算需要显示控件的总宽度
width += headers[c].Width;
if (cols[c].Visible == false)
this.PrintWidth / width);
cols[c].Width = headers[c].Width;
// 设置需要显示的非第一列坐标,应该为前一列坐标加上宽度
lines[c].End = lines[c].Start;
第三步:运行报表,在运行报表之前需要指定用户选择的列:
源码下载: