Add new fields using c # code to crystal report (rpt) - c#

I have a crystal report rpt report created with some fixed fields that fill with a datatable, the fields obtained in the datatable may vary depending on the SQL statement of the fields in a form. Therefore, the question is whether in the design of the rpt form I can add, by code, new fields obtained in the datatable. For example, in the datatable I get the First and Last Name fields, and I show them in the report (in the design I have inserted the First and Last Name fields). Now in the datatable I get Name, Surname and Telephone, if I call the same report, it will fill in only Name and Surname, the Telephone field, not being inserted in the report will not appear, the idea is to add it. I am filling the dataset in such a way:
DataSet ds = new dsDataSet();
ds.Tables.Add(new DataTable());
foreach (DataColumn column in dtBusqueda.Columns)
{
ds.Tables[0].Columns.Add(column.ColumnName);
}
Now it would be those dataset fields to insert into the report as I explained before. The report is loaded as follows:
ReportDocument Report = new ReportDocument();
Report.Load("../../crReporte.rpt");
Report.SetDataSource(dtDataTable);
frmReportes form = new frmReportes(Report);
form.Show();
Thanks.

Crystal work only with pre-defined data object to display the data in a specified format. So if you pass a different data object then it will give you errors that it cannot locate your column.
I can offer :
1) Use same dataTable source for all (put tel column in original). So set visibility on Crystal Report for your special column. with supress.
2) You can create individual 2 different report And in your If Case you can load another report template if you want.
if it is only tel number not for complex requirement. use way 1. just set visibility.
But if you have complex requirement. I think you should use 2 different report template.

Related

Crystal report unlinked Subreport

I have a crystal report with several parts
Page Header a (Company Information)
Page Header b (Sub-Report: has list of specific countries)
Page Header c (Sub-Report: has list of specific Products)
Page Header d (Sub-Report: has list of specific items)
The data source of the report is given by a Dataset "DS1" through C# Application (Push Method)
Because of the complex logic in the background, it is decided to have Dataset "DS1" with 4 Data tables (tbl0,tbl1,tbl2,tbl3) to hold separately the correct data in the Dataset.
The dataset doesn't have any relationships, because I have loaded directly the final data result in the relative Data Tables in the Dataset
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
DataTable dt3 = new DataTable();
DataTable dt4 = new DataTable();
Load_CompanyInfo(country_ID, ref dt1);
Load_Countries(_ApprovalID, ref dt2);
Load_Products(_ApprovalID, ref dt3);
Load_Items(_ApprovalID, ref dt4);
_ds_tmp.Tables.Add(dt1);
_ds_tmp.Tables.Add(dt2);
_ds_tmp.Tables.Add(dt3);
_ds_tmp.Tables.Add(dt4);
In the Form ReportView.cs, I have assigned the Dataset to the report.
rep_c_single.SetDataSource(ds.Tables[0]); // Main company info
rep_c_single.Subreports[0].SetDataSource(ds.Tables[1]); // countries
rep_c_single.Subreports[1].SetDataSource(ds.Tables[2]); // products
rep_c_single.Subreports[2].SetDataSource(ds.Tables[3]); // items
rep_c_single.Refresh();
cr_Viewer.ReportSource = rep_c_single;
on the report file, the sub reports doesn't have link field to the main report.
Because final Data is prepared in the Data Tables.
I just want to display the Data in the Sub report independent from the Main Report data source.
What I have till now, the sub reports lists all countries and all products, not what in the data table exists.
EDIT:
It is important to mention: the sub reports and main reports don't have any relations. I want only to PUSH data to the relative Sub-reports
thanks for any help
i'm not entirely sure.. but i've done this once..
make sure your report has a group based on the primary key of the main report (with which you want to link all the sub reports)
right click on the sub report
change sub report links
in the available fields select the column name of the sub report or respective data table
click on add to add it into the fields to link
it'll also show in the sub-report paramater to use drop down list underneath
check the flag -- select data in sub-report based on field
select the main report primary key group you made in the first step
Hope it works.. :)

How to make a Crystal report with data from classes?

I am new to this tool, so I do not know how I can get the data of a class and show in a report.
I don't have a database, my info is stored inside variables inside classes. Like this (Folder Objetos):
My program does: Read a XML, validate with XSD, serialize in this classes.
What I want: Take that information and show in a report.
It is possible to put my data from variables in a Crystal report? Where I start?
well crystal report can take many things as " DataSource" ranging from object, datatable, dataset etc etc. without looking at the code it is hard to figure out where you are having issue.
basic syntax for crystalreport load datasource is
CrystalDecisions.CrystalReports.Engine.ReportDocument reportDoc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
reportDoc.Load("your RPT file");
reportDoc.SetDataSource(object)
here instead of object you can use dataset, datatable etc.\
Is this what you were looking for ?
Added
reportDoc.SetDataSource(ds.Tables["YourTableName"])
Also one more thing to note is if you report requires lets say 20 fields they all must be preset in the datatable. or else it will not work. if it has any extra fields in dataset it will just ignore those

Tablix not showing DataSet created using a "custom" IEnumerable

I want to print a report based on the datasource list of a DataGridView.
I want to use Rdlc reports for this. I created the report, put a tablix in it with a DataSet named "DataSet".
Then, in the report viewer, I just delete that DataSet, and add a DataSet with the same name that contains the list(IEnumerable) I wanted. The problem is that the Tablix is not showing anything. I looked in the internet but can´t find anything related to it. Also, how can I make fields visible or invisible using the ReportViewer?
This is my code (it is in the Form´s Load event:
Sistema_financiero_Entities db = new Sistema_financiero_Entities();
IEnumerable<Cheque> cheques = db.Cheque.Where(x => x.id_cheque != 6).ToList();
ReportDataSource data = new ReportDataSource("DataSet", cheques);
try
{
this.rpt_Viewer.LocalReport.DataSources.Add(data);
this.rpt_Viewer.RefreshReport();
}
Some considerations: db is the Entity framework object context. cheques is the list (in this example I´m just taking data from the db, but I will use the DataGridView datasource list, I did this just to test if this was working). rpt_Viewer is the reportViewer.
Changing IEnumerable<Cheque> cheques = db.Cheque.Where(x => x.id_cheque != 6).ToList();
to
List<Cheque> cheques = db.Cheque.Where(x => x.id_cheque != 6).ToList(); worked perfectly.

Dynamically add rows of data to a *.rpt file (C#)

I am using vs2008 with CrystalReports and I'm wandering how can i dynamically add rows of data to a *.rpt file?(using c#).
In more detail i want to create a a function that populates a *.rpt file with data that might contain lists(for example "FirstName", "LastName", List<"Friend"> ;..Friend beeing an object that contains multiple fields like "FriendNr", "Address",....).
the code that i used so far is:
ReportDocument rpt = new ReportDocument();
MemoryStream stream = new MemoryStream();
string filename = filepath + "/myRpt.rpt";
rpt.Load(filename);
rpt.SetParameterValue(0, myObject.FirstName);
rpt.SetParameterValue(1, myObject.LastName);
Inside the rpt file i have placed FieldObjects(Parameter Fields), and i populate the file with data by assigning the desired values to these objects (" rpt.SetParameterValue(0, myObject.FirstName);" )
Please help me find a way to populate the report with the rows of data contained in the List also.
Thanks a lot for your time.
I don't think it is possible to add data rows to a report this way. I suggest using a Typed DataSet as your report data source. The report can then display as many Friend objects as you require.
Dynamic 'rows' approaches:
1). You could add more items to the parameter's CurrentValues collection. Not sure how you are using this in the report, but it may work for your purposes. Look at the ParameterFieldDefinition class for more information.
2). Create a DataSet, modify as necessary, then assign it to the report. Use the ReportDocument.SetDataSource() method to bind a report to data programmatically.
3). Another approach is to build a report that uses XML data, then programmatically modify the XML, then refresh the report.

How to create an unbound subreport in another unbound report

I have an unbound XtraReport that has a subreport control which contains another report. I call "unbound" to a report that has the definition of the fields using a schema but not actually bound to any DataSet, I create a DataTable using a Data Access Layer and then pass that object to the DataSource property of the report.
So, I have the following code:
// (...) Get the data from the db and fill a DataTable
if (table.Rows.Count > 0)
{
report.DataSource = table;
// (...) Get the data from the db and fill a DataTable for the subreport
report.SubPurchaseOrder.Report.DataSource = tableSubReport;
report.ShowPreviewDialog();
}
else
{
MessageBox.Show("No data to show.");
}
But what I get using this approach is the subreport printed very oddly (take a look at the attached pdf, sorry it's in spanish but I think you get the idea).
I've read the DevExpress documentation and maybe I'm not getting the approach right, so my question to you is how to create a report that has one or more subreports but I have to provide the data to fill them using some process external to the reports, such as a Data Access Layer?
Please let me know if the question is not stated correctly or lacks of more info.
EDIT:
I uploaded a sample project with the report with problem here.
I've tried to use parameters of some kind. In the BeforePrint event of the subreport control, I tried:
((XRSubreport)sender).ReportSource.FilterString = "[IdPO_RO] = " + _idPurchaseOrder;
and
((XRSubreport)sender).ReportSource.Parameters["Id"].Value = _idPurchaseOrder;
Of course, for the second, I added a parameter and the filter string the same as the first but using the parameter.
I could solve the problem.
The cause for this was that I was assigning to the wrong object. This line:
report.SubPurchaseOrder.Report.DataSource = tableSubReport;
should be:
report.SubPurchaseOrder.ReportSource.DataSource = tableSubReport;
So the brief explanation is that I was using another property to refer to the report contained in the subreport control (XRSubreport).

Categories

Resources