动态创建Crystal Report

时间:2014-03-31 07:07:00

标签: sql-server vb.net crystal-reports-2008

我想在运行时通过从树视图中选择报告的字段来创建Crystal Report。树视图由表名和列名作为子节点组成。从树视图中选择列名后,必须单击“生成报告”按钮,这将显示所选字段的Crystal Report。我怎么能这样做?

Public Class Form1

Dim objRpt As CrystalReport1
Dim con As New SqlConnection
Private Function CreateSelectQueryAndParameters() As String

    Dim reportDocument As New ReportDocument
    Dim paramFields As New ParameterFields
    Dim paramField As ParameterField
    Dim paramDiscreteValue As ParameterDiscreteValue
    Dim query As String = "SELECT "

    Dim columnNo As Integer = 0

    If CheckBox1.Checked Then

        columnNo = columnNo + 1

        query = query.Insert(query.Length, "pcode as Column" + columnNo.ToString())

        paramField = New ParameterField()
        paramField.Name = "col" + columnNo.ToString()
        paramDiscreteValue = New ParameterDiscreteValue()
        paramDiscreteValue.Value = "Property Code"
        paramField.CurrentValues.Add(paramDiscreteValue)

        paramFields.Add(paramField)
    End If

    If CheckBox2.Checked Then

        columnNo = columnNo + 1
        If (query.Contains("Column")) Then

            query = query.Insert(query.Length, ", ")
        End If

        query = query.Insert(query.Length, "pname as Column" +
        columnNo.ToString())

        paramField = New ParameterField()
        paramField.Name = "col" + columnNo.ToString()
        paramDiscreteValue = New ParameterDiscreteValue()
        paramDiscreteValue.Value = "Property Name"
        paramField.CurrentValues.Add(paramDiscreteValue)
        paramFields.Add(paramField)
    End If
    For i As Integer = columnNo To 2


        columnNo = columnNo + 1

        paramField = New ParameterField()
        paramField.Name = "col" + columnNo.ToString()
        paramDiscreteValue = New ParameterDiscreteValue()
        paramDiscreteValue.Value = ""
        paramField.CurrentValues.Add(paramDiscreteValue)

        paramFields.Add(paramField)
    Next

    CrystalReportViewer1.ParameterFieldInfo = paramFields

    query += " FROM propdb"
    Return query

End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    objRpt = New CrystalReport1()
    con.ConnectionString = "Data Source=MY-PC; Initial Catalog=hrmdb; Integrated Security=True"
    con.Open()
    Dim query As String = CreateSelectQueryAndParameters()
    If Not query.Contains("Column") Then
        MessageBox.Show("No selection to display!")
        Return
    End If

    Try
        **' I DON'T KNOW WHAT CODE TO ADD HERE'**

        CrystalReportViewer1.ReportSource = objRpt

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

    con.Close()

End Sub
End Class

我在C#中有一个类似的程序,但他们使用了Access,我现在正在使用SQL Server:

public partial class Form1 : Form
{
    CrystalReport1 objRpt;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        objRpt = new CrystalReport1();

        string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\db1.mdb"; 

        //Get Select query Strring and add parameters to the 
        //Crystal report.
        string query = CreateSelectQueryAndParameters();

        //if there is no item select,then exit from the method.
        if (!query.Contains("Column"))
        {
            MessageBox.Show("No selection to display!");
            return;
        }

        try
        {
            OleDbConnection Conn = new OleDbConnection(connString);

            OleDbDataAdapter adepter = new OleDbDataAdapter(query, connString);
            DataSet1 Ds = new DataSet1();

            adepter.Fill(Ds, "Customer");

            objRpt.SetDataSource(Ds);
            crystalReportViewer1.ReportSource = objRpt;
        }
        catch (OleDbException oleEx)
        {
            MessageBox.Show(oleEx.Message);
        }
        catch (Exception Ex)
        {
            MessageBox.Show(Ex.Message);
        }
    }

    /// <summary>
    /// This method is used to 
    /// 1. create SELECT query according to the selected column names and 
    /// 2. create parameters and assign values for that parameter that correspond to 
    ///    the crystal report.
    /// NOTE: This parameter is used to display column names of the Crystal Report
    /// according to the user selection.
    /// </summary>
    /// <returns></returns>
    private string CreateSelectQueryAndParameters()
    {
        ReportDocument reportDocument;
        ParameterFields paramFields;

        ParameterField paramField;
        ParameterDiscreteValue paramDiscreteValue;

        reportDocument = new ReportDocument();
        paramFields = new ParameterFields();

        string query = "SELECT ";
        int columnNo = 0;                

        if (chbCode.Checked)
        {
            columnNo++;
            query = query.Insert(query.Length, "Code as Column" + columnNo.ToString());

            paramField = new ParameterField();
            paramField.Name = "col" + columnNo.ToString();
            paramDiscreteValue = new ParameterDiscreteValue();
            paramDiscreteValue.Value = "Customer Code";
            paramField.CurrentValues.Add(paramDiscreteValue);
            //Add the paramField to paramFields
            paramFields.Add(paramField);
        }

        if (chbFirstName.Checked)
        {
            columnNo++;

            if (query.Contains("Column"))
            {
                query = query.Insert(query.Length, ", ");
            }

            query = query.Insert(query.Length, "FirstName as Column" + columnNo.ToString());

            paramField = new ParameterField();
            paramField.Name = "col" + columnNo.ToString();
            paramDiscreteValue = new ParameterDiscreteValue();
            paramDiscreteValue.Value = "First Name";
            paramField.CurrentValues.Add(paramDiscreteValue);
            //Add the paramField to paramFields
            paramFields.Add(paramField);
        }

        if (chbLastName.Checked)
        {
            columnNo++;

            if (query.Contains("Column"))
            {
                query = query.Insert(query.Length, ", ");
            }

            query = query.Insert(query.Length, "LastName as Column" + columnNo.ToString());

            paramField = new ParameterField();
            paramField.Name = "col" + columnNo.ToString();
            paramDiscreteValue = new ParameterDiscreteValue();
            paramDiscreteValue.Value = "Last Name";
            paramField.CurrentValues.Add(paramDiscreteValue);

            // Add the paramField to paramFields
            paramFields.Add(paramField);
        }

        if (chbAddress.Checked)
        {
            columnNo++;

            if (query.Contains("Column"))
            {
                query = query.Insert(query.Length, ", ");
            }

            query = query.Insert(query.Length, "Address as Column" + columnNo.ToString());

            paramField = new ParameterField();
            paramField.Name = "col" + columnNo.ToString();
            paramDiscreteValue = new ParameterDiscreteValue();
            paramDiscreteValue.Value = "Address";
            paramField.CurrentValues.Add(paramDiscreteValue);
            //Add the paramField to paramFields
            paramFields.Add(paramField);
        }

        if (chbPhone.Checked)
        {
            columnNo++;

            if (query.Contains("Column"))
            {
                query = query.Insert(query.Length, ", ");
            }

            query = query.Insert(query.Length, "Phone as Column" + columnNo.ToString());

            paramField = new ParameterField();
            paramField.Name = "col" + columnNo.ToString();
            paramDiscreteValue = new ParameterDiscreteValue();
            paramDiscreteValue.Value = "Phone";
            paramField.CurrentValues.Add(paramDiscreteValue);

            // Add the paramField to paramFields
            paramFields.Add(paramField);
        }

        //if there is any remaining parameter, assign empty value for that 
        //parameter.
        for (int i = columnNo; i < 5; i++)
        {
            columnNo++;
            paramField = new ParameterField();
            paramField.Name = "col" + columnNo.ToString();
            paramDiscreteValue = new ParameterDiscreteValue();
            paramDiscreteValue.Value = "";
            paramField.CurrentValues.Add(paramDiscreteValue);
            //Add the paramField to paramFields
            paramFields.Add(paramField);
        }

        crystalReportViewer1.ParameterFieldInfo = paramFields;

        query += " FROM Customer" ;
        return query;
    }
}

0 个答案:

没有答案