如何在C#中从Win Forms中选择的日期参数显示水晶报表

时间:2016-09-04 12:16:20

标签: c# crystal-reports

  • 当我定义日期和日期参数时,我的报告显示为空白。
  • 我的报告来源是XML文件。

记录报告

enter image description here

private void button1_Click(object sender, EventArgs e)
  {
        TR = Application.OpenForms.OfType<Records>().ElementAt(0);
        try
        {
            ds = new DataSet2();
            dt = new System.Data.DataTable();
            dt = ConvertDGVtoDataTable(TR.recordsTableDataGridView);
            ds.Tables.Add(dt);
            ds.WriteXmlSchema("RecordsReport.xml");

        try
        {

            ParameterFieldDefinitions crParameterFieldDefinitions;
            ParameterFieldDefinition crParameterFieldDefinition;
            ParameterValues crParameterValues = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();

            crParameterDiscreteValue.Value = dateTimePicker1.Value;
            crParameterFieldDefinitions = treport1.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["fromDate"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;

            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

            crParameterDiscreteValue.Value = dateTimePicker2.Value;
            crParameterFieldDefinitions = treport1.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["toDate"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;

            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);

        // Report Form.
        TReportDisplay TRD = new TReportDisplay();
        Treport treport1 = new Treport();

            TRD.crystalReportViewer2.ReportSource = treport1;
            treport1.SetDataSource(ds);
            TRD.Show();
            TRD.crystalReportViewer2.Refresh();
        }
        catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); };
    }

    catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }
}

我的报告有两个参数, fromDate toDate ,两者都定义为日期时间, 谨慎的价值观。 enter image description here

我的选择记录公式如下:

enter image description here

如果我删除此公式,则报告将显示所有内容,但如果我输入此公式,则报告将显示为空白。

已解决:在我的情况下,我不会将数据库值用作“Arthi在此处出现” Filter data in crystal report using datetimepicker

1 个答案:

答案 0 :(得分:1)

而不是dateTimePicker1.Value,请尝试以下方法:

crParameterDiscreteValue.Value = dateTimePicker1.Text;

请参阅此内容 - Filter data in crystal report using datetimepicker