从文本框将值传递给Crystal Report Header

时间:2014-06-17 10:54:02

标签: c# crystal-reports

我创建一个水晶报告并且工作正常但我的问题是将文本框值传递给水晶报告标题

要生成报告我使用两个日期时间选择器来获取两个日期之间的报告我的问题是在水晶报告标题上打印出这两个日期

像  报告从'txtfrom.text'到'txtto.text'

这是我的代码

protected void Button2_Click1(object sender, EventArgs e)
{
    SqlConnection cnn = new SqlConnection("Data Source=YCC-DB-2K8;Initial Catalog=yccwebportal;User ID=sa;Password=p@ssw0rd");
    string sql = "select * from View_EQtype where Purchase_date between '"+ txtfrom.Text + "'AND'" + txtto.Text+"'";
    DataSet ds = new DataSet();

    SqlCommand cmd = new SqlCommand(sql, cnn);
    cmd.CommandType = CommandType.Text;
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    adp.Fill(ds, "View_EQtype");
    cnn.Open();
    ReportDocument myReportDocument;
    myReportDocument = new ReportDocument();
    myReportDocument.Load("C:/inetpub/wwwroot/yenwebportal/EQTypeReport.rpt");
    myReportDocument.SetDataSource(ds);
    myReportDocument.SetDatabaseLogon("sa", "p@ssw0rd");
    CrystalReportViewer1.ReportSource = myReportDocument;
    CrystalReportViewer1.DisplayToolbar = true;
    CrystalReportViewer1.ToolPanelView = CrystalDecisions.Web.ToolPanelViewType.None;   
}

3 个答案:

答案 0 :(得分:0)

使用报告参数 创建两个参数Names(FromDate),(ToDate) 并使用

发送参数值
//paramter from
    ParameterFieldDefinitions crParameterFieldDefinitions;
            ParameterFieldDefinition crParameterFieldDefinition;
            ParameterValues crParameterValues = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
            crParameterDiscreteValue.Value ="";//Desire Date From
            crParameterFieldDefinitions = EQTypeReport.rpt.DataDefinition.ParameterFields;
            crParameterFieldDefinition = crParameterFieldDefinitions["FromDate"];
            crParameterValues = crParameterFieldDefinition.CurrentValues;
            crParameterValues.Clear();
            crParameterValues.Add(crParameterDiscreteValue);
            crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
            //paramter DateTo
            ParameterFieldDefinitions crParameterFieldDefinitions2;
            ParameterFieldDefinition crParameterFieldDefinition2;
            ParameterValues crParameterValues2 = new ParameterValues();
            ParameterDiscreteValue crParameterDiscreteValue2 = new ParameterDiscreteValue();
            crParameterDiscreteValue2.Value = ""; //Desire Date To
            crParameterFieldDefinitions2 = EQTypeReport.rpt.DataDefinition.ParameterFields;
            crParameterFieldDefinition2 = crParameterFieldDefinitions2["ToDate"];
            crParameterValues2 = crParameterFieldDefinition2.CurrentValues;
            crParameterValues2.Clear();
            crParameterValues2.Add(crParameterDiscreteValue2);
            crParameterFieldDefinition2.ApplyCurrentValues(crParameterValues2); 

然后在报告中使用参数

答案 1 :(得分:0)

在报告中创建两个参数字段,并将日期设置为:

        myReportDocument.SetParameterValue("@FromDate", DateToString(RadDatePicker1.SelectedDate));

        myReportDocument.SetParameterValue("@ToDate", DateToString(RadDatePicker2.SelectedDate));

或者像这样: 您的数据源是数据表

myReportDocument = new ReportDocument();
myReportDocument.Load(Server.MapPath(RPTName));
DT.Columns.Add("From_Date");
DT.Columns.Add("To_Date");
DT.Rows(0).Item("From_Date") = DateToString(RadDatePicker1.SelectedDate);
DT.Rows(0).Item("To_Date") = DateToString(RadDatePicker2.SelectedDate);
myReportDocument.SetDataSource(DT);

它们将作为归档对象出现在字段资源管理器中

答案 2 :(得分:0)

如果我做得对,你想将代码中的值传递给Crystal Report的文本对象。假设您的报告中包含txtFromDate和txtToDate文本对象:

CrystalDecisions.CrystalReports.Engine.TextObject txtFromDate = mainReport.ReportDefinition.ReportObjects["txtFromDate "] as TextObject;
txtFromDate .Text = DateTimePickerFrom.SelectedValue.ToString();

CrystalDecisions.CrystalReports.Engine.TextObject txtToDate = mainReport.ReportDefinition.ReportObjects["txtToDate "] as TextObject;
txtToDate.Text = DateTimePickerTo.SelectedValue.ToString();

因此,您需要创建文本对象的实例。如果你不想这样,我在报告中使用了拖放这些参数的相同内容:

enter image description here

我希望这对你有所帮助。