前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >根据数据源字段动态设置报表中的列数量以及列宽度

根据数据源字段动态设置报表中的列数量以及列宽度

作者头像
葡萄城控件
发布2018-01-10 17:06:58
4.7K0
发布2018-01-10 17:06:58
举报

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

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

image
image

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

代码语言:js
复制
    /// <summary>
    /// 用户选择的列名称
    /// </summary>
    public List<string> Columns;

    private void Report1_ReportStart(object sender, EventArgs e)
    {
        // 定义临时变量
        int count = 0;
        float width = 0;            
        Label tmp = null;

        // 列头控件
        List<Label> headers = new List<Label>();
        headers.Add(this.label1);
        headers.Add(this.label2);
        headers.Add(this.label3);
        headers.Add(this.label4);
        headers.Add(this.label5);
        headers.Add(this.label6);
        headers.Add(this.label7);
        headers.Add(this.label8);

        // 数据控件
        List<TextBox> cols = new List<TextBox>();
        cols.Add(this.textBox1);
        cols.Add(this.textBox2);
        cols.Add(this.textBox3);
        cols.Add(this.textBox4);
        cols.Add(this.textBox5);
        cols.Add(this.textBox6);
        cols.Add(this.textBox7);
        cols.Add(this.textBox8);

        List<CrossSectionLine> lines = new List<CrossSectionLine>();
        lines.Add(crossSectionLine1);
        lines.Add(crossSectionLine2);
        lines.Add(crossSectionLine3);
        lines.Add(crossSectionLine4);
        lines.Add(crossSectionLine5);
        lines.Add(crossSectionLine6);
        lines.Add(crossSectionLine7);

        // 隐藏不需要显示的控件,并计算需要显示控件的总宽度
        for (int c = 0; c < cols.Count; c++)
        {
            if (!Columns.Contains(cols[c].DataField))
            {
                headers[c].Visible = false;
                cols[c].Visible = false;
                if (c < cols.Count - 1)
                {
                    lines[c].Visible = false;
                }
            }
            else
            {
                width += headers[c].Width;
            }
        }

        // 调整列的位置以及宽度
        for (int c = 0; c < cols.Count; c++)
        {
            // 隐藏控件不需要处理
            if (cols[c].Visible == false)
            {
                continue;
            }

            headers[c].Width = headers[c].Width * (this.PrintWidth / width);
            cols[c].Width = headers[c].Width;

            // 设置控件坐标
            if (tmp == null)
            {
                // 设置需要显示的第一列坐标
                headers[c].Location = new PointF(0, headers[c].Location.Y);
                cols[c].Location = new PointF(headers[c].Location.X, cols[c].Location.Y);
            }
            else
            {
                // 设置需要显示的非第一列坐标,应该为前一列坐标加上宽度
                headers[c].Location = new PointF(tmp.Location.X + tmp.Width, headers[c].Location.Y);
                cols[c].Location = new PointF(headers[c].Location.X, cols[c].Location.Y);
            }


            // 调整边线位置
            if (c < headers.Count - 1)
            {
                lines[c].Start = new PointF(headers[c].Location.X + headers[c].Width, lines[c].Start.Y);
                lines[c].End = lines[c].Start;
            }
            count += 1;
            tmp = headers[c];
        }
    }

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

DynamicColumns
DynamicColumns

源码下载:

动态设置报表中的列数量以及列宽度

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-07-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
腾讯云 BI
腾讯云 BI(Business Intelligence,BI)提供从数据源接入、数据建模到数据可视化分析全流程的BI能力,帮助经营者快速获取决策数据依据。系统采用敏捷自助式设计,使用者仅需通过简单拖拽即可完成原本复杂的报表开发过程,并支持报表的分享、推送等企业协作场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档