当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Crystal Reports Programatically sort using two passed in parameters

问题描述:

I am pretty new to Crystal Reports and I am probably not using the latest. We are using VS2010/ASP.NET as our main programming environment but we use the integrated Crystal Reports 2008 designer in VS2008, so I need to switch to 2008 when designing reports. I have been passing in paramaters to reports by defining the Parameter Field in the IDE and then passing them in. We have ASP.NET screens with GridViews that are sortable. The data is displayed on the Crystal Report but I have to match how it is sorted. I looked things up on Google and found this and this article. I can't figure out how to use these code snippets. Our best bet seems to be our "Report Controller" classes which is the only place in C# where the Crystal Reports objects are available. A class looks like the following:

public class CRMOCContactsController : ReportingBase

{

public CRMOCContactsController(DataSet reportData, NameValueCollection reportParams)

{

// Create an instance of the Crystal Report.

this.Report = new Reports.CRMOCContacts();

// Get the data

this.ReportData = reportData;

foreach (string s in reportParams.AllKeys)

{

CRHelper.SetCurrentValuesForParameterField(this.Report.ParameterFields, s, reportParams[s]);

}

}

protected override void SetDataSource()

{

this.Report.Database.Tables["ContactRecord"].SetDataSource(this.ReportData.Tables["ContactRecord"]);

this.Report.Database.Tables["ContactSearchCriteria"].SetDataSource(this.ReportData.Tables["ContactSearchCriteria"]);

this.Report.Database.Tables["SSIFields"].SetDataSource(this.ReportData.Tables["SSIFields"]);

}

Does anyone see how I could use these type of classes to do this? one parameter would be the field to sort and another would be the direction. Thanks. Showing me in code really helps.

Tiago, my browser is locked down and doesn't let me add a comment. The only place there is Crystal code is in autogenerated C# classes that go with each report. For instance, the Reports.CRMOCContacts() class referenced above. That is autogenerated by a tool and can't be modified:

//------------------------------------------------------------------------------

//

// This code was generated by a tool.

// Runtime Version:2.0.50727.3603

//

// Changes to this file may cause incorrect behavior and will be lost if

// the code is regenerated.

//

//------------------------------------------------------------------------------

There is the only reference to ReportDocument I see:

public virtual CrystalDecisions.CrystalReports.Engine.ReportDocument CreateReport() {

CRMOCContacts rpt = new CRMOCContacts();

rpt.Site = this.Site;

return rpt;

}

I figured something out. The code in CRMOCContactsController can be Crystal code if I treat this.Report as a ReportDocument and add these libraries:

using CrystalDecisions.Shared;

using CrystalDecisions.ReportSource;

using CrystalDecisions.CrystalReports.Engine;

Then I could do:

FieldDefinition FieldDef = null;;

FieldDef = this.Report.Database.Tables[0].Fields[];

this.Report.DataDefinition.SortFields[0].Field = FieldDef;

this.Report.DataDefinition.SortFields[0].SortDirection = CrystalDecisions.Shared.SortDirection.AscendingOrder;

I just don't know how to pass in the sortField? What format?

网友答案:

Sam, I couldn't figure out how your helper classes are working. It looks like you are passing a dataset to the ReportDocument, right? If this the case, you can pass to the ReportDocument sorted datatables, and it will keep the order of lines.

Here, I am passing a dataset to my report, created from a stored procedures that receives the order by as a parameter. The dataset comes sorted, so the report shows the information sorted as well.

分享给朋友:
您可能感兴趣的文章:
随机阅读: