Click here to Skip to main content
16,022,542 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using C# windows application. I want to add a Dynamic column in Crystal report at run time. Because I dont know about the columns in design time. The columns will come based on the sql stored procedure.
So i want to Add a column to Crystal report at run time.
Posted
Updated 18-Jan-10 20:38pm
v3

Hi victimized dude,
Here I've a solution for your problem hope it will be useful.
All you need to care about is passing the parameter to the report via the ParameterField in CrystalReport.
Have a look to the code below


//This is the name of the Crystal report
AgentLicenceCardCollectively1 report = new AgentLicenceCardCollectively1();
ParameterFields myParams = new ParameterFields();
ParameterField paramOfficerName = new ParameterField();
ParameterField paramOfficerPost = new ParameterField();
ParameterField paramCompanyId = new ParameterField();
ParameterField paramALICO = new ParameterField();

ParameterDiscreteValue myDiscreteValue1 = new ParameterDiscreteValue();
ParameterDiscreteValue myDiscreteValue2 = new ParameterDiscreteValue();
ParameterDiscreteValue myDiscreteValue3 = new ParameterDiscreteValue();
ParameterDiscreteValue myDiscreteValue4 = new ParameterDiscreteValue();

// Set the ParameterFieldName to the name of the parameter
// created in the Field Explore
paramOfficerName.ParameterFieldName = "officerName";
paramOfficerPost.ParameterFieldName = "officerPost";
paramCompanyId.ParameterFieldName = "companyId";
paramALICO.ParameterFieldName = "ALICO";
myDiscreteValue1.Value = cboOfficerName.Text;
paramOfficerName.CurrentValues.Add(myDiscreteValue1);
myDiscreteValue2.Value = cboPost.Text;
paramOfficerPost.CurrentValues.Add(myDiscreteValue2);
myDiscreteValue3.Value = cboCompanyName.Text.Substring(0, 2);
paramCompanyId.CurrentValues.Add(myDiscreteValue3);
myDiscreteValue4.Value = "";

// Add param object to params collection
myParams.Add(paramOfficerName);
myParams.Add(paramOfficerPost);
myParams.Add(paramCompanyId);
myParams.Add(paramALICO);

crystalReportViewer1.ParameterFieldInfo = myParams;
report.SetDataSource(dataSet);

crystalReportViewer1.ReportSource = report;

Enjoy
-Satyam182
 
Share this answer
 
based on you additional details, looks like you need to display data fetched from DB.

Use query or dataset as the dataprovider to your report and then in design view of crystal you can use 'Table' as one of the dataregion. Bind the columns of values expected from DB.

Now if 5 values returned you will get 5 rows (in the table in UI at runtime) and if 3 then 3 rows.

Hope that helps.
P.S: you will need crystal designer for it. (its not free - its licensed!)
 
Share this answer
 
YOU CANNOT DO THIS ...
:-D HE HE HE
SQL
I am using C# windows application. I want to add a Dynamic column in Crystal report at run time. Because I dont know about the columns in design time. The columns will come based on the sql stored procedure.
So i want to Add a column to Crystal report at run time.
 
Share this answer
 
While binding data Runtime, the performance of Crystal control is not that great compared with binding data though queries.
 
Share this answer
 
May be This will give u an Idea , or may be i dont fully understand the question
C#
private void ReportContentStyler(ReportDocument MyRep)
 {
     int padding = 1070;
     for (int i = int.Parse(Session["StartFromdic"].ToString()); i <= 11; i++)
     {
         MyRep.ReportDefinition.Sections[1].ReportObjects["Parm" + i.ToString() + "1"].ObjectFormat.EnableSuppress = true;
         int myWidth = MyRep.ReportDefinition.Sections[1].ReportObjects[0].Width;
         MyRep.ReportDefinition.Sections[1].ReportObjects[0].Left =
             MyRep.ReportDefinition.Sections[1].ReportObjects[0].Left + padding;
         MyRep.ReportDefinition.Sections[1].ReportObjects[1].Left =
            MyRep.ReportDefinition.Sections[1].ReportObjects[1].Left + padding;
         MyRep.ReportDefinition.Sections[2].ReportObjects["Line23"].Left = MyRep.ReportDefinition.Sections[2].ReportObjects["Line23"].Left + padding;
         MyRep.ReportDefinition.Sections[3].ReportObjects["Line25"].Left = MyRep.ReportDefinition.Sections[2].ReportObjects["Line25"].Left + padding;

         MyRep.ReportDefinition.Sections[3].ReportObjects["Line28"].Left = MyRep.ReportDefinition.Sections[2].ReportObjects["Line28"].Left + padding;

     }
     int lineID = 20;
     for (int x = 0; x <= (11 - int.Parse(Session["StartFromdic"].ToString())); x++)
     {
         MyRep.ReportDefinition.Sections[3].ReportObjects["Line" + lineID.ToString()].ObjectFormat.EnableSuppress = true;
         lineID--;
     }
     int allLeft = MyRep.ReportDefinition.Sections[1].ReportObjects[0].Left;
     int width = MyRep.ReportDefinition.Sections[1].ReportObjects[0].Width;
     allLeft = allLeft / 2;

     foreach (CrystalDecisions.CrystalReports.Engine.Section sec in MyRep.ReportDefinition.Sections)
     {
         if (sec.Name != MyRep.ReportDefinition.Sections[0].Name && sec.Name != MyRep.ReportDefinition.Sections[4].Name)
         {
             foreach (CrystalDecisions.CrystalReports.Engine.ReportObject obj in sec.ReportObjects)
             {
                 try
                 {
                     if (!obj.ObjectFormat.EnableSuppress)
                     {

                         obj.Left = obj.Left - allLeft;
                         ((CrystalDecisions.CrystalReports.Engine.DrawingObject)(obj)).Right = ((CrystalDecisions.CrystalReports.Engine.DrawingObject)(obj)).Right - allLeft;
                     }

                 }
                 catch (Exception ex)
                 {
                     try
                     {

                         if (obj.Kind == ReportObjectKind.LineObject)
                         {

                             int top = ((LineObject)obj).Top;
                             int right = ((LineObject)obj).Right;
                             ((LineObject)obj).Top = ((LineObject)obj).Bottom;
                             ((LineObject)obj).Right = obj.Left - allLeft;
                             ((LineObject)obj).Left = obj.Left - allLeft;
                             ((LineObject)obj).Top = top;
                             ((LineObject)obj).LineStyle = LineStyle.SingleLine;


                         }
                     }
                     catch (Exception ex2)
                     {


                     }

                 }

             }
         }
     }
 }
 
Share this answer
 
Some lines cant be moved, need to use some hack.

VB
Private Class Line

    Public Shared Sub Move(ByVal Line As Object, ByVal HorizontalMove As Integer)
        'Taken from IL decompile
        Dim Hack = Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Instance
        Dim RasDocument = Line.GetType().GetProperty("RasDocument", Hack).GetValue(Line, Nothing)
        Dim RasReportObject = Line.GetType().GetProperty("RasObject", Hack).GetValue(Line, Nothing)
        Dim iSCRReportObject = RasReportObject.Clone(True)
        iSCRReportObject.Left += HorizontalMove
        iSCRReportObject.Right += HorizontalMove
        RasDocument.ReportDefController.ReportObjectController.Modify(RasReportObject, iSCRReportObject)
    End Sub

End Class
 
Share this answer
 
I dont want to add runtime parameter i want to generate report runtime
suppose there are 3 values coming from database then those three value should be display in report and suppose there are five values coming from database those five values should be display in crystal report.
 
Share this answer
 
Dear Deepaji,
I'm very close enough for your problem. can u please mention your DB and the exact scenario of your project. If you are using MySQL 5.0 as I am doing. Then You definitely at hard time as the MySql Bridge is not so much mature enough. However if you're using Microsoft based DB, then you can create DataTable within DataSet,fill the DataTable with dynamic fields and use the Table afterward in the report.
-Satyam
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900