如何在水晶报告中打印日期时间选择器?

时间:2015-07-27 09:55:35

标签: c# winforms crystal-reports datetimepicker

ComponentXProperties

这是针对datagridview到crystal报表的。我还想将dtpFrom和dtpTo的datimepicker值显示给crystal Report。

1 个答案:

答案 0 :(得分:1)

首先通过 Crystal Reports - >在Crystal报表的某处添加文本对象 插入 - > 文字对象。文本对象的默认名称是 Text1 。您可以在文本对象属性下看到它。如果需要,您可以更改此名称。现在,在您的C#代码中,您需要传递Time的值。

示例:

ReportDocument rptDoc = new ReportDocument();

protected void Page_Init(object sender, EventArgs e)
{
    // Load the report path
    string reportPath = Server.MapPath("~/CrystalReportTime.rpt");
    rptDoc.Load(reportPath);
    CrystalReportViewer1.ReportSource = rptDoc; 
}

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        string reportPath = Server.MapPath("~/CrystalReportTime.rpt");
        rptDoc.Load(reportPath);
        CrystalReportViewer1.ReportSource = rptDoc;
    }

    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    dt.Columns.Add("Name", typeof(String));      
    dt.Columns.Add("Class", typeof(String));
    dt.Columns.Add("RollNo", typeof(String));
    foreach (DataGridViewRow dgr in dgv_Student.Rows)
    {
      dt.Rows.Add(dgr.Cells["Name"].Value, dgr.Cells["Class"].Value, dgr.Cells["RollNo"].Value);
    }
    ds.Tables.Add(dt); 
    rptDoc.SetDataSource(ds);

    // Pass the time to the Crystal Reports text object
    CrystalDecisions.CrystalReports.Engine.TextObject Text1;
    Text1 = rptDoc.ReportDefinition.ReportObjects["Text1"] as TextObject;
    Text1.Text = "Time from " + String.Format("{0:MMMM dd, yyyy}", Convert.ToDateTime(dtpFrom)) + " to " + String.Format("{0:MMMM dd, yyyy}", Convert.ToDateTime(dtpTo));
}

protected void Page_UnLoad(object sender, EventArgs e)
{
    this.CrystalReportViewer1.Dispose();
    this.CrystalReportViewer1 = null;
    rptDoc.Close();
    rptDoc.Dispose();
    rptDoc = null;
    GC.Collect();
}

希望这有帮助。