文章出處

在報表系統中,我們通常會有這樣的需求,就是由用戶來決定報表中需要顯示的數據,比如數據源中共有八列數據,用戶可以自己選擇在報表中顯示哪些列,并且能夠自動調整列的寬度,已鋪滿整個頁面。本文就講解一下ActiveReports中該功能的實現方法。

第一步:設計包含所有列的報表模板,將數據源中的所有列先放置到報表設計界面,并設置你需要的列寬,最終界面如下:

image

第二步:在報表的后臺代碼中添加一個Columns的屬性,用于接收用戶選擇的列,同時,在報表的ReportStart事件中添加以下代碼:

    /// <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

 

源碼下載:

 

 

動態設置報表中的列數量以及列寬度


文章列表


不含病毒。www.avast.com
全站熱搜
創作者介紹
創作者 大師兄 的頭像
大師兄

IT工程師數位筆記本

大師兄 發表在 痞客邦 留言(0) 人氣()