Tuesday, July 2, 2013

Set value in Text Object in Crystal report at Runtime in C#

Here is the Simple Code :-
===================

CrystalReport1 Obj = new CrysralReport1();

 //Set a Crystal Reports text object at runtime
 CrystalDecisions.CrystalReports.Engine.TextObject txtQuotationNum = ((CrystalDecisions.CrystalReports.Engine.TextObject)crQuotation.ReportDefinition.Sections["Section1"]
.ReportObjects["txtQuotation"]);  // "txtQuotation" is the TextObject name mention on Crystal Report Viewer.

  txtQuotationNum.Text = "bla bla bla...";
  crystalReportViewer1.ReportSource = crQuotation;

3 comments:

  1. I'm trying to pass a value to my text object..
    The value will be passed when the submit button is clicked..
    I follow your code but the "crystalReportViewer1" can't be found..

    So I did a little modification like below :
    displayCR_form cr = new displayCR_form();
    CrystalReportViewer CRv = new CrystalReportViewer();
    TaxInvoice TI = new TaxInvoice();

    TextObject TaxInvNo = (TextObject)TI.ReportDefinition.Sections["Section2"].ReportObjects["TaxInvoice_No"];
    TaxInvNo.Text = txtTaxInvoiceNo.Text.ToString();
    CRv.ReportSource = TI;

    cr.Refresh();
    cr.ShowDialog();

    However the text object was still empty..
    What did I miss? Please kindly help.. Thank you in advance..

    ReplyDelete
    Replies
    1. i studied your code..
      you are missing to setDatasource in your text object. thats why your text object dont have any value.
      first bind the data in datatable which is to be send to the text object.
      try this example :

      DataTable dtSlctData = new dataTable();
      dtSlctData.Columns.Add("name");
      DataRow drow = new DataRow();
      drow[0] = "bla bla bla....";
      dtSlctData .Rows.Add(drow);
      CrystalReport2 objReport = new CrystalReport2();
      objReport.SetDataSource(dtSlctData);
      TextObject value= (TextObject)objReport.ReportDefinition.ReportObjects["txtGradeValueSystem"];
      value.Text = "bla bla bla...";
      crystalReportViewer1.ReportSource = objReport;

      crystalReportViewer1.Refresh();

      Delete