Using Crystal report in C# step by step

In this tutorial we will see how to create and print Crystal Reports in C# Windows Forms application.

Using Crystal report in C# step by step

First, add a preview button in the main form Form1, and in the 2nd form Form2 add CrystalReportViewer and set its Modifiers property to Public:

Using Crystal report in C# step by step
Using Crystal report in C# step by step

Then add a blank Crystal Report to the project:

Using Crystal report in C# step by step

Choose Database fields from Field Explorer, browse to your Database file, then drag and drop the fields you want to show in the report like this:

Using Crystal report in C# step by step

Finally, add this code to PreviewButton click event:

private void PreviewButton_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.Show();

    if (con.State != ConnectionState.Open)
    {
        con.Open();
    }

    string s = "SELECT * FROM Employee";
    OleDbCommand cmd = new OleDbCommand(s, con);
    OleDbDataAdapter adap = new OleDbDataAdapter(cmd);
    DataSet ds = new DataSet();
    adap.Fill(ds, "Employee");
    CrystalReport1 cr1 = new CrystalReport1();
    cr1.SetDataSource(ds);
    f2.crystalReportViewer1.ReportSource = cr1;
    f2.crystalReportViewer1.Refresh();
    con.Close();
}

For more explanation, watch this video:




Using Crystal report in C# step by step Using Crystal report in C# step by step Reviewed by Bloggeur DZ on 03:34 Rating: 5

3 commentaires:

Fourni par Blogger.