Thursday, December 18, 2014

Custom Columns select using Crystal Report (Selected Columns to Be DISPLAYED IN CRYSTALREPORT AT RUN TIME)

First create a Data Set

Click on Add Button
It prompts for a message saying as




Click on Yes Button











ADD  YOUR COLUMNS  AS COLUMN1..........  COLUMNN



Add New Item



Select crystal report.rpt file and the below screen is visible



And Click on Ok button

And then press ctrl +alt +T


And click on  Ok BUTTON

In the Report Viewer Screen 

Press Ctrl+Alt +T 

In the Report 
Parameter Fields (Right Click and Select New)

Give the Name as col1

And Similarly Add 
Col2,col3,col4,
Similarly Add the Formula Fields as Below


And finally your database fields looks like 


Drag the controls 


.cs page

Add the following Namespaces
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.ReportSource;
using CrystalDecisions.Shared;


write the following code in button click


------------------------------------------------------------------------------------
private string GetData()
    {
        ReportDocument reportDocument;
        ParameterFields paramFields;

        ParameterField paramField;
        ParameterDiscreteValue paramDiscreteValue;

        reportDocument = new ReportDocument();
        paramFields = new ParameterFields();

        string query = "SELECT ";
        int columnNo = 0;

        if (ChkEmpID.Checked)
        {
            columnNo++;
            query = query.Insert(query.Length, "EmpID as Column" + columnNo.ToString());


            paramField = new ParameterField();
            paramField.Name = "col" + columnNo.ToString();
            paramDiscreteValue = new ParameterDiscreteValue();
            paramDiscreteValue.Value = "EmpID";
            paramField.CurrentValues.Add(paramDiscreteValue);
            //Add the paramField to paramFields
            paramFields.Add(paramField);
        }
        if (ChkEmpName.Checked)
        {
            columnNo++;
            if (query.Contains("Column"))
            {
                query = query.Insert(query.Length, ", ");
            }
            query = query.Insert(query.Length, "Ename as Column" + columnNo.ToString());

            paramField = new ParameterField();
            paramField.Name = "col" + columnNo.ToString();
            paramDiscreteValue = new ParameterDiscreteValue();
            paramDiscreteValue.Value = "Ename";
            paramField.CurrentValues.Add(paramDiscreteValue);
            //Add the paramField to paramFields
            paramFields.Add(paramField);
        }
        if (ChkDesignation.Checked)
        {
            columnNo++;
            if (query.Contains("Column"))
            {
                query = query.Insert(query.Length, ", ");
            }
            query = query.Insert(query.Length, "Designation as Column" + columnNo.ToString());


            paramField = new ParameterField();
            paramField.Name = "col" + columnNo.ToString();
            paramDiscreteValue = new ParameterDiscreteValue();
            paramDiscreteValue.Value = "Designation";
            paramField.CurrentValues.Add(paramDiscreteValue);
            //Add the paramField to paramFields
            paramFields.Add(paramField);
        }
        if (chksalary.Checked)
        {
            columnNo++;
            if (query.Contains("Column"))
            {
                query = query.Insert(query.Length, ", ");
            }
            query = query.Insert(query.Length, "Salary as Column" + columnNo.ToString());


            paramField = new ParameterField();
            paramField.Name = "col" + columnNo.ToString();
            paramDiscreteValue = new ParameterDiscreteValue();
            paramDiscreteValue.Value = "Salary";
            paramField.CurrentValues.Add(paramDiscreteValue);
            //Add the paramField to paramFields
            paramFields.Add(paramField);
        }

        CrystalReportViewer1.ParameterFieldInfo = paramFields;
        query += " FROM Employee";
        return query;
    }


 OUTPUT





No comments:

Post a Comment

Thank you for visiting my blog

Kubernetes

Prerequisites We assume anyone who wants to understand Kubernetes should have an understating of how the Docker works, how the Docker images...