在报表系统中,我们通常会有这样的需求,就是由用户来决定报表中需要显示的数据,比如数据源中共有八列数据,用户可以自己选择在报表中显示哪些列,并且能够自动调整列的宽度,已铺满整个页面。本文就讲解一下该功能的实现方法。

在报表系统中,我们通常会有这样的需求,就是由用户来决定报表中需要显示的数据,比如数据源中共有八列数据,用户可以自己选择在报表中显示哪些列,并且能够自动调整列的宽度,已铺满整个页面。本文就讲解一下​​ActiveReports​​中该功能的实现方法。

第一步:设计包含所有列的报表模板,将数据源中的所有列先放置到报表设计界面,并设置你需要的列宽,最终界面如下:

​​

第二步:在报表的后台代码中添加一个Columns的属性,用于接收用户选择的列,同时,在报表的ReportStart事件中添加以下代码:

/// <summary>
/// 用户选择的列名称
/// </summary>
public List<string> Columns;
private void Report1_ReportStart(object
{
// 定义临时变量
int
float
null;
// 列头控件
new
this.label1);
this.label2);
this.label3);
this.label4);
this.label5);
this.label6);
this.label7);
this.label8);
// 数据控件
new
this.textBox1);
this.textBox2);
this.textBox3);
this.textBox4);
this.textBox5);
this.textBox6);
this.textBox7);
this.textBox8);
new
lines.Add(crossSectionLine1);
lines.Add(crossSectionLine2);
lines.Add(crossSectionLine3);
lines.Add(crossSectionLine4);
lines.Add(crossSectionLine5);
lines.Add(crossSectionLine6);
lines.Add(crossSectionLine7);
// 隐藏不需要显示的控件,并计算需要显示控件的总宽度
for (int
{
if
{
false;
false;
if
{
false;
}
}
else
{
width += headers[c].Width;
}
}
// 调整列的位置以及宽度
for (int
{
// 隐藏控件不需要处理
if (cols[c].Visible == false)
{
continue;
}
this.PrintWidth / width);
cols[c].Width = headers[c].Width;
// 设置控件坐标
if (tmp == null)
{
// 设置需要显示的第一列坐标
new
new
}
else
{
// 设置需要显示的非第一列坐标,应该为前一列坐标加上宽度
new
new
}
// 调整边线位置
if
{
new
lines[c].End = lines[c].Start;
}
count += 1;
tmp = headers[c];
}
}

第三步:运行报表,在运行报表之前需要指定用户选择的列:

 

​​

 

源码下载: