Click here to Skip to main content
15,891,529 members
Articles / Programming Languages / C#

Custom Field in Crystal Report

Rate me:
Please Sign up or sign in to vote.
4.75/5 (17 votes)
6 Aug 2008CPOL3 min read 116K   5.9K   45  
This application will adjust the position of the fields in Crystal report, which are dragged and dropped during design time
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using HMIS.Main.Registration_Comm;
using CrystalDecisions.CrystalReports;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace CustomeCrystalReportFields
{
    public partial class PatientRerport2 : Form
    {
        #region CLASS VARIABLES
        private DateTime n;
        private DateTime dt;
        private DataTable dtable;
        private int totalMales = 0, totalFemales = 0;
        private ParameterField paramField = new ParameterField();
        private ParameterFields paramFields = new ParameterFields();
        private ParameterDiscreteValue discValue = new ParameterDiscreteValue();
        //FOR FUNCTIONS
        bool isFullNameSet = false;
        bool isRelationshipSet = false;
        bool isGenderSet = false;
        bool isAgeSet = false;
        bool isAddressSet = false;

        //FOR FIELDS
        bool contactNo = false;
        bool nicno = false;
        bool address = false;
        bool age = false;
        bool gender = false;
        
        #region CRYSTAL REPORT RELATED VARIABLES TO SET DYNAMICALLY
        private bool showFullName = false;
        private bool showRelationship = false;
        private bool showGender = false;
        private bool showAge = false;
        private bool showAddress = false;
        private bool showContact = false;
        private bool showNICNo = false;
        private bool showAgeGraph = false;
        private bool showGenderGraph = false;

       
        #endregion
        #endregion

        #region CLASS CONSTRUCTOR
        public PatientRerport2()
        {
            InitializeComponent();
        }
        #endregion
            
        #region showReportBtn_Click Event Handler
        private void showReportBtn_Click(object sender, EventArgs e)
        {
            
            try
            {
                n = fromDateTimePicker.Value;
                dt = new DateTime(n.Year, n.Month, n.Day);
                //label5.Text=dt.ToString();
                n = toDateTimePicker.Value;
                dtable = Registration.GetPatientsByRegDate(dt, new DateTime(n.Year, n.Month, n.Day, 23, 59, 59));
                for (int i = 0; i < dtable.Rows.Count; i++)
                {
                    if (dtable.Rows[i]["isMale"].ToString().ToLower().Equals("true"))
                        //dtable.Rows[i]["Photo"] = HMIS.Global.ImageConverter.ByteToImgConvert((byte[])dtable.Rows[i]["Photo"]);
                        totalMales += 1;
                    else
                        totalFemales += 1;
                }
                //label5.Text += "   " + new DateTime(n.Year, n.Month, n.Day, 23, 59, 59).ToString();
            }
            catch (Exception ee)
            {
                MessageBox.Show("Error Occured! Please Try Again");
                return;
            }

            if (dtable.Rows.Count == 0)
            {
                MessageBox.Show("No Record Found against your search Criteria. Try Again!","Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                crystalReportViewer1.ReportSource = null;
                return;
            }
            groupBox2.Visible = false;
            GBCustomReport.Visible = true;
            this.AcceptButton = btnShowReport;

            #region CODE BY USMAN DASTGEER
            //PageSetupDialog p=new PageSetupDialog();
            //System.Drawing.Printing.PrintDocument pp = new System.Drawing.Printing.PrintDocument();
            //p.Document = pp;
            //p.ShowDialog();
            //ParameterFields fields = new ParameterFields();
            //ParameterField field = new ParameterField();
            //ParameterDiscreteValue value = new ParameterDiscreteValue();
            //field.Name = "@fromDate";
            //value.Value = dt;
            //field.CurrentValues.Add(value);
            //fields.Add(field);

            //field = new ParameterField();
            //field.Name = "@toDate";
            //value.Value = new DateTime(n.Year, n.Month, n.Day, 23, 59, 59);
            //field.CurrentValues.Add(value);
            //fields.Add(field);
            //crystalReportViewer1.ParameterFieldInfo = fields;
            //dataGridView1.DataSource = dtable;
            #endregion
        }
        #endregion

        #region toDateTimePicker_ValueChanged Event Handler
        private void toDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            if (fromDateTimePicker.Value > toDateTimePicker.Value)
            {
                MessageBox.Show("To Date must not be less than From DateTime");
                fromDateTimePicker.Value = toDateTimePicker.Value;
            }
        }
        #endregion

        #region fromDateTimePicker_ValueChanged Event Handler
        private void fromDateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            if (fromDateTimePicker.Value > toDateTimePicker.Value)
            {
                MessageBox.Show("To Date must not be less than From DateTime");
                fromDateTimePicker.Value = toDateTimePicker.Value;
            }
        }
        #endregion

        #region btnShowReport_Click Event Handler
        private void btnShowReport_Click(object sender, EventArgs e)
        {
            if (CBListField.CheckedItems.Count == 0)
            {
                MessageBox.Show("Select the fields to display", "Informaiton", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }           

          #region GETTING ALL THE CHECKED FIELDS IN CHECKBOXLIST CONTROL
            
            for (int i = 0; i < CBListField.Items.Count;i++)
            {
                if (CBListField.GetItemChecked(i))
                {
                    if (CBListField.Items[i].ToString().Replace(" ","").ToLower().Equals("fullname"))
                    {
                        showFullName = true;
                    }
                    else
                        if (CBListField.Items[i].ToString().Replace(" ","").ToLower().Equals("relationshipname"))
                        {
                            showRelationship = true;
                        }
                        else
                            if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("gender"))
                            {
                                showGender = true;
                            }
                        else
                                if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("age"))
                            {
                                showAge = true;
                            }
                            else
                                if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("address"))
                                {
                                    showAddress = true;
                                }
                                else
                                    if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("contactnumber"))
                                    {
                                        showContact = true;
                                    }
                                    else
                                        if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("nic_no"))
                                        {
                                            showNICNo = true;
                                        }
                                        else
                                            if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("agegraph"))
                                            {
                                                showAgeGraph = true;
                                            }
                                            else
                                                if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("gendergraph"))
                                                {
                                                    showGenderGraph = true;
                                                }
                }
            }
           
            #endregion

            ShowReport(dtable, totalMales, totalFemales, ref paramField, paramFields, ref discValue);
            
        }
     
        #endregion

        #region ShowReport Function, with nested adjustFields for Crystal Report Function
        private void ShowReport(DataTable dtable, int totalMales, int totalFemales, ref ParameterField paramField, ParameterFields paramFields, ref ParameterDiscreteValue discValue)
        {
            crystalReportViewer1.ReportSource = null;
            crystalReport21.Close();

            #region CODE WRITTEN BY KASHIF ABBAS FOR PASSING PARAMETER VALUES TO CRYSTAL REPORT
            paramField.Name = "TotalPatient";
            discValue.Value = dtable.Rows.Count;
            paramField.CurrentValues.Add(discValue);
            paramFields.Add(paramField);


            discValue = new ParameterDiscreteValue();
            paramField = new ParameterField();
            paramField.Name = "TotalMales";
            discValue.Value = totalMales;
            paramField.CurrentValues.Add(discValue);
            paramFields.Add(paramField);

            discValue = new ParameterDiscreteValue();
            paramField = new ParameterField();
            paramField.Name = "TotalFemales";
            discValue.Value = totalFemales;
            paramField.CurrentValues.Add(discValue);
            paramFields.Add(paramField);

            discValue = new ParameterDiscreteValue();
            paramField = new ParameterField();
            paramField.Name = "fromDateString";
            discValue.Value = fromDateTimePicker.Value.ToShortDateString().ToString();
            paramField.CurrentValues.Add(discValue);
            paramFields.Add(paramField);

            discValue = new ParameterDiscreteValue();
            paramField = new ParameterField();
            paramField.Name = "toDateString";
            discValue.Value = toDateTimePicker.Value.ToShortDateString().ToString();
            paramField.CurrentValues.Add(discValue);
            paramFields.Add(paramField);
            #endregion

            this.crystalReportViewer1.ParameterFieldInfo = paramFields;
            crystalReport21.SetDataSource(dtable);
            crystalReportViewer1.ReportSource = crystalReport21;
            showFields();
            setFieldsState();
            adjustFieldsonCR();

            //}
        }
        private void showFields()
        {
            #region CODE WRITTEN BY KASHIF ABBAS TO CONDITIONALY SHOWING THE SELECTED FIELDS
            if (!showFullName)
            {
                crystalReport21.Section2.ReportObjects["FullName1"].ObjectFormat.EnableSuppress = true;
                crystalReport21.Section2.ReportObjects["FullNameText"].ObjectFormat.EnableSuppress = true;
            }
            if (!showRelationship)
            {
                crystalReport21.Section2.ReportObjects["Relationship1"].ObjectFormat.EnableSuppress = true;
                crystalReport21.Section2.ReportObjects["RelationshipFullName1"].ObjectFormat.EnableSuppress = true;
                crystalReport21.Section2.ReportObjects["RelationShipNameText"].ObjectFormat.EnableSuppress = true;
                crystalReport21.Section2.ReportObjects["Text9"].ObjectFormat.EnableSuppress = true;
            }
            if (!showGender)
            {
                crystalReport21.Section2.ReportObjects["MaleCheckBox1"].ObjectFormat.EnableSuppress = true;
                crystalReport21.Section2.ReportObjects["GenderText"].ObjectFormat.EnableSuppress = true;
            }
            if (!showAge)
            {
                crystalReport21.Section2.ReportObjects["age1"].ObjectFormat.EnableSuppress = true;
                crystalReport21.Section2.ReportObjects["AgeText"].ObjectFormat.EnableSuppress = true;
            }
            if (!showAddress)
            {
                crystalReport21.Section2.ReportObjects["address2"].ObjectFormat.EnableSuppress = true;
                crystalReport21.Section2.ReportObjects["AddressText"].ObjectFormat.EnableSuppress = true;
            }
            if (!showNICNo)
            {
                crystalReport21.Section2.ReportObjects["nicno1"].ObjectFormat.EnableSuppress = true;
                crystalReport21.Section2.ReportObjects["NICNoText"].ObjectFormat.EnableSuppress = true;
            }
            if (!showContact)
            {
                crystalReport21.Section2.ReportObjects["contactnumber1"].ObjectFormat.EnableSuppress = true;
                crystalReport21.Section2.ReportObjects["ContactNumberText"].ObjectFormat.EnableSuppress = true;
            }

            if (!showGenderGraph)
            {
                crystalReport21.Section2.ReportObjects["gendergraph"].ObjectFormat.EnableSuppress = true;
            }
            if (!showAgeGraph)
            {
                crystalReport21.Section2.ReportObjects["agegraph"].ObjectFormat.EnableSuppress = true;
            }
            #endregion
        }
        #region adjustFieldsonCR and setFieldsState Functions
        private void setFieldsState()
        {
            isFullNameSet = false;
            isRelationshipSet = false;
            isGenderSet = false;
            isAgeSet = false;
            isAddressSet = false;
            contactNo = false;
            nicno = false;
            address = false;
            age = false;
            gender = false;
        }
        private void adjustFieldsonCR()
        {
            
            if (crystalReport21.ReportDefinition.Sections[2].ReportObjects["fullname1"].ObjectFormat.EnableSuppress && !isFullNameSet)
            {
                FullNameSuppress();
                isFullNameSet = true;
            }
            if (crystalReport21.ReportDefinition.Sections[2].ReportObjects["relationship1"].ObjectFormat.EnableSuppress && !isRelationshipSet)
            {
                    RelationShipSuppress();
                    isRelationshipSet = true;
                
            }
            if (crystalReport21.ReportDefinition.Sections[2].ReportObjects["MaleCheckBox1"].ObjectFormat.EnableSuppress && !isGenderSet)
            {
                    GenderSuppress();
                    isGenderSet = true;
            }
            if (crystalReport21.ReportDefinition.Sections[2].ReportObjects["age1"].ObjectFormat.EnableSuppress && !isAgeSet)
            {
                AgeSuppress();
                isAgeSet = true;
            }
            if (crystalReport21.ReportDefinition.Sections[2].ReportObjects["Address2"].ObjectFormat.EnableSuppress && !isAddressSet)
            {
                AddressSuppress();
                isAddressSet = true;
            }


        }
        #endregion

        #region IF AGE'S FIELD VISIBILITY IS FALSE THEN THIS CODE WILL EXECUTE
        private void AgeSuppress()
        {
            SetSuppressFields("age1", "AgeText");
        }
         #endregion

        #region IF GENDER'S FIELD VISIBILITY IS FALSE THEN THIS CODE WILL EXECUTE
        private void GenderSuppress()
        {            
            SetSuppressFields("MaleCheckBox1", "genderText");
        }
        #endregion

        #region IF ADDRESS'S FIELD VISIBILITY IS FALSE THEN THIS CODE WILL EXECUTE
        private void AddressSuppress()
        {
            SetSuppressFields("Address2", "AddressText");
        }
        #endregion

        #region IF RELATIONSHIP'S VISIBILITY IS FALSE THEN THIS CODE WILL EXECUTE
        private void RelationShipSuppress()
        {
            SetSuppressFields("RelationshipNameText", "Text9");
        }
        #endregion

        #region IF FULLNAME'S VISIBILITY IS FALSE THEN THIS CODE WILL EXECUTE
        private void FullNameSuppress()
        {
            SetSuppressFields("fullname1", "fullnametext");
        }
        #endregion

        #region SetSuppressFields
        private void SetSuppressFields(string fieldName, string fieldText)
        {
            if (!crystalReport21.ReportDefinition.Sections[2].ReportObjects["contactnumber1"].ObjectFormat.EnableSuppress && !contactNo)
            {
                crystalReport21.ReportDefinition.Sections[2].ReportObjects["contactnumber1"].Left = crystalReport21.ReportDefinition.Sections[2].ReportObjects[fieldName].Left;
                crystalReport21.ReportDefinition.Sections[2].ReportObjects["contactnumbertext"].Left = crystalReport21.ReportDefinition.Sections[2].ReportObjects[fieldText].Left;
                contactNo = true;
            }
            else
                if (!crystalReport21.ReportDefinition.Sections[2].ReportObjects["nicno1"].ObjectFormat.EnableSuppress && !nicno)
                {
                    crystalReport21.ReportDefinition.Sections[2].ReportObjects["nicno1"].Left = crystalReport21.ReportDefinition.Sections[2].ReportObjects[fieldName].Left;
                    crystalReport21.ReportDefinition.Sections[2].ReportObjects["nicnotext"].Left = crystalReport21.ReportDefinition.Sections[2].ReportObjects[fieldText].Left;
                    nicno = true;
                }
                else
                    if (!crystalReport21.ReportDefinition.Sections[2].ReportObjects["address2"].ObjectFormat.EnableSuppress && !address)
                    {
                        crystalReport21.ReportDefinition.Sections[2].ReportObjects["address2"].Left = crystalReport21.ReportDefinition.Sections[2].ReportObjects[fieldName].Left;
                        crystalReport21.ReportDefinition.Sections[2].ReportObjects["addresstext"].Left = crystalReport21.ReportDefinition.Sections[2].ReportObjects[fieldText].Left;
                        address = true;
                    }
                    else
                        if (!crystalReport21.ReportDefinition.Sections[2].ReportObjects["age1"].ObjectFormat.EnableSuppress && !age)
                        {
                            crystalReport21.ReportDefinition.Sections[2].ReportObjects["age1"].Left = crystalReport21.ReportDefinition.Sections[2].ReportObjects[fieldName].Left;
                            crystalReport21.ReportDefinition.Sections[2].ReportObjects["agetext"].Left = crystalReport21.ReportDefinition.Sections[2].ReportObjects[fieldText].Left;
                            age = true;
                        }
                        else
                            if (!crystalReport21.ReportDefinition.Sections[2].ReportObjects["malecheckbox1"].ObjectFormat.EnableSuppress && !gender)
                            {
                                crystalReport21.ReportDefinition.Sections[2].ReportObjects["malecheckbox1"].Left = crystalReport21.ReportDefinition.Sections[2].ReportObjects[fieldName].Left;
                                crystalReport21.ReportDefinition.Sections[2].ReportObjects["Gendertext"].Left = crystalReport21.ReportDefinition.Sections[2].ReportObjects[fieldText].Left;
                                gender = true;
                            }
        }
        #endregion
           #endregion

        #region btnNewSearch_Click Event Handler, when the Previous Button is Clicked
        private void btnNewSearch_Click(object sender, EventArgs e)
        {

            this.crystalReport21.Close();
            this.crystalReportViewer1.ReportSource = null;
            groupBox2.Visible = true;
            groupBox2.Enabled = true;
            groupBox2.BringToFront();
            GBCustomReport.SendToBack();
            CBListField.ClearSelected();
            for (int i = 0; i < CBListField.Items.Count; i++)
            {
                CBListField.SetItemChecked(i, false);
            }
            this.AcceptButton = showReportBtn;
        }
        #endregion

        #region CBListField_SelectedIndexChanged Event Hanlder
        private void CBListField_SelectedIndexChanged(object sender, EventArgs e)
        {
            isFullNameSet = false;
            isRelationshipSet = false;
            isGenderSet = false;
            isAgeSet = false;
            isAddressSet = false;
            contactNo = false;
            nicno = false;
            address = false;
            age = false;
            gender = false;
            
            #region GETTING ALL THE CHECKED FIELDS IN CHECKBOXLIST CONTROL

            for (int i = 0; i < CBListField.Items.Count; i++)
            {
                if (!CBListField.GetItemChecked(i))
                {
                    if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("fullname"))
                    {
                        showFullName = false;
                    }
                    else
                        if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("relationshipname"))
                        {
                            showRelationship = false;
                        }
                        else
                            if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("gender"))
                            {
                                showGender = false;
                            }
                            else
                                if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("age"))
                                {
                                    showAge = false;
                                    
                                }
                                else
                                    if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("address"))
                                    {
                                        showAddress = false;
                                       
                                    }
                                    else
                                        if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("contactnumber"))
                                        {
                                            showContact = false;
                                            
                                        }
                                        else
                                            if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("nic_no"))
                                            {
                                                showNICNo = false;
                                                
                                            }
                                            else
                                                if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("agegraph"))
                                                {
                                                    showAgeGraph = false;
                                                }
                                                else
                                                    if (CBListField.Items[i].ToString().Replace(" ", "").ToLower().Equals("gendergraph"))
                                                    {
                                                        showGenderGraph = false;
                                                    }
                }
            }
            //if (!String.IsNullOrEmpty(UnselectedFields))
            //{
            //    UnselectedFields = UnselectedFields.Remove(UnselectedFields.Length - 1);


            //string[] fieldsToShow = UnselectedFields.Split(',');
            //string deleteColumnName = "";
            //foreach (string str in fieldsToShow)
            //{
            //    deleteColumnName = str;
            //    if (str.Contains(" "))
            //    {
            //        deleteColumnName = str.Replace(" ", "");
            //    }
            //    dtable.Columns.Remove(deleteColumnName);
            //}
            ////dtable.Columns.Remove(dtable.Columns["fullname"]);
            //}
            #endregion
        }
        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer (Senior) Ciklum
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions