August 31, 2021 08:58 by
Peter
Most of the time while developing reports people wonder how to have alternating row colors in the report like below.
There is a cool trick to it.
Go to the format section of the Details. Click the Color Tab. Then click the button next to the Background Color CheckBox.
In the formula editor of background color, type the following:
if RecordNumber mod 2 = 0 then crSilver else crNoColor.
Save and close the editor.
And now when you run the report you will see alternate colors of silver and white in the details section.
August 6, 2021 08:42 by
Peter
You can print a Crystal Report using the print option of Crystal Report Viewer. However, there are occasions when you want your application to print a report directly to the printer without viewing the report in Crystal Report Viewer.
The ReportDocument class provides PrintToPrinter method that may be used to print a CR direct to the printer. If no printer is selected, the default printer will be used to send the printing pages to.
The PrintToPrinter method takes four parameters.
nCopies : Indicates the number of copies to print.
collated : Indicates whether to collate the pages.
startPageN : Indicates the first page to print.
endPageN : Indicates the last page to print.
The following steps will guide you to achieve the same:
Add a crystal report (.cr) file to your ASP.NET application.
Add a report instance on the page level.
Dim report As MyReport = New MyReport
Populate reports data on Page_Init
' Get data in a DataSet or DataTable
Dim ds As DataSet = GetData()
' Fill report with the data
report.SetDataSource(ds)
Print Report
report.PrintToPrinter(1, False, 0, 0)
If you wish to print a certain page range, change the last two parameters From To page number.
If you want to set page margins, you need to create a PageMargin object and set PrintOptions of the ReportDocument.
The following code sets page margins and printer name:
Dim margins As PageMargins = Report.PrintOptions.PageMargins
margins.bottomMargin = 200
margins.leftMargin = 200
margins.rightMargin = 50
margins.topMargin = 100
Report.PrintOptions.ApplyPageMargins(margins)
' Select the printer name
Report.PrintOptions.PrinterName = printerName
September 12, 2019 12:22 by
Peter
I have a DBClass that is responsible for executing database queries and SPs.
Here is the code snippet that shows how to use a background thread to get data and generate reports. This is useful when the main thread of your applications is doing something else, data entry and you want reports to be generated in the background.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace myCryst
{
public partial class Form1 : Form
{
DataSet ds;
frmCryst f = new frmCryst();
public Form1()
{
InitializeComponent();
}
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
bgWorker.ReportProgress(10);
Thread.Sleep(164);
bgWorker.ReportProgress(20);
Thread.Sleep(164);
bgWorker.ReportProgress(30);
Thread.Sleep(164);
ds = DBClass.ExecQuery("select * from students");
bgWorker.ReportProgress(40);
Thread.Sleep(164);
bgWorker.ReportProgress(50);
Thread.Sleep(164);
bgWorker.ReportProgress(70);
Thread.Sleep(164);
bgWorker.ReportProgress(95);
Thread.Sleep(164);
bgWorker.ReportProgress(100);
}
private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}
private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
showCrystalReport();
}
private void button1_Click(object sender, EventArgs e)
{
bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgressChanged);
bgWorker.WorkerReportsProgress = true;
bgWorker.RunWorkerAsync();
}
private void showCrystalReport()
{
CrystalReport1 c = new CrystalReport1();
Reportds d = new Reportds();
d.Tables[0].Merge(ds.Tables["tab"]);
frmCryst f = new frmCryst();
c.SetDataSource(d);
f.crystRep.ReportSource = c;
f.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}