Filter data in crystal report using datetimepicker

前端 未结 1 448
栀梦
栀梦 2021-01-14 21:12

I\'m working on a report in Crystal Reports C# windows application.i fetch the data from localhost server to crystal report. I have 2 datetimepicker in my form.

date

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-14 21:38

    @divi

    filter data in report using datetimepicker is not done for your coding

    Do the following:

    create a new parameter fromdate, todate in Field Explorer -> Parameter Field(Right Click)-> New

    like this make todate also

    then right click the report.

    Select Report -> Selection Formula -> Record...

    Do following

    then click save and close button in that form.

    Know this:

    Right Click the report -> Database -> Set Datasource Location...

    Now Code

     ReportDocument report = new ReportDocument();
                report.Load("C:\\Users\\Desktop\\CrystalReport1.rpt");
    
                TableLogOnInfo Table = new TableLogOnInfo();
                ConnectionInfo Connection = new ConnectionInfo();
                Tables Tables;
    
                ParameterFieldDefinitions Parameters;
                ParameterFieldDefinition Parameter;
                ParameterValues Values = new ParameterValues();
                ParameterDiscreteValue DiscreteValue = new ParameterDiscreteValue();
    
                DiscreteValue.Value = dateTimePicker1.Text;
                Parameters = report.DataDefinition.ParameterFields;
                Parameter = Parameters["fromdate"];
                Values = Parameter.CurrentValues;
    
                Values.Clear();
                Values.Add(DiscreteValue);
                Parameter.ApplyCurrentValues(Values);
    
                DiscreteValue.Value = dateTimePicker2.Text;
                Parameters = report.DataDefinition.ParameterFields;
                Parameter = Parameters["todate"];
                Values = Parameter.CurrentValues;
    
                Values.Add(DiscreteValue);
                Parameter.ApplyCurrentValues(Values);
    
                Connection.ServerName = "Your servername in Set Datasource Location";
                Connection.DatabaseName = "Your databasename in Set Datasource Location";
                Connection.UserID = "your username";
                Connection.Password = "your password";
    
                Tables = report.Database.Tables;
                foreach (CrystalDecisions.CrystalReports.Engine.Table table in Tables)
                {
                    Table = table.LogOnInfo;
                    Table.ConnectionInfo = Connection;
                    table.ApplyLogOnInfo(Table);
                }
    
                crystalReportViewer1.ReportSource = report;
                crystalReportViewer1.Refresh();
    

    Hope This helps to solve your problem.

    0 讨论(0)
提交回复
热议问题