Search code examples
c#wpfcrystal-reports

Crystal Report With Visual Studio 2017 WPF


How can I pass data to my crystal report viewer. From My MainWindow.cs. I am using Visual Studio 2017. My Crystal Report file name is CrystalReport1.rpt. My DataSet is Id and Description.

private void CrystalReport_Load(object sender, EventArgs e)
    {
        CrystalReport1 crystalReport = new CrystalReport1();

        string LotNumber = "78545755";

        DataSet1 dataSet1 = new DataSet1();
        DataTable reportDataTable = dataSet1.DataTable1;
        DataRow lotRow = reportDataTable.NewRow();
        lotRow["Id"] = 1;
        lotRow["Description"] = LotNumber;

        reportDataTable.Rows.Add(lotRow);



        dataSet1.BeginInit();

        crystalReport.SetDataSource(dataSet1);

        var cystalWindow = new CrystalReportsViewer();

        cystalWindow.ViewerCore.ReportSource = crystalReport;
    }

Solution

  • After the SetDataSource() step pass a parameter value like this:

    myReportObject.SetParameterValue("MyParameterName", StringValueforParameter);
    

    You obviously need to have a parameter as part of the design of the report. To create a parameter, right-click the 'Parameter Fields' node in Crystal's field explorer and select 'New...'.