Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / XML

DataGridView to Crystal Report in C#

4.65/5 (14 votes)
6 Jun 2013CPOL2 min read 119.3K   6.3K  
this tip discuss about showing datagridview data to crystal report using c#

Introduction

When we have data in a DataGridView and want to convert the data to Crystal Report, we can do it by transferring the DataGridView data to XML format by using a Dataset and a Datatable. In this tip, I created a dataset and a datatable within the dataset and defined columns for the datatable same as the DataGridView columns. So we can convert the datatable to an XML file and then we use this XML file for Crystal Reports.

Background

Remember that to use Crystal Reports, we shall change the app.config file format and use .NET Framework 4 (not .NET Framework 4 client profile) by right clicking on the project in Solution Explorer and selecting Properties.

Change the app.config file to this format:

XML
<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>

Using the code

  1. We have a DataGridView with three columns: id (integer), name (string), and family (string), and a button for converting the DataGridView to Crystal Report and I use the data in the form load event for showing in DataGridView (you can use databinding to a SQL Server database):
    C#
    private void Form1_Load(object sender, EventArgs e)
    {
        //create table with three columns
        DataTable t = new DataTable();
        t.Columns.Add("id", typeof(Int16));
        t.Columns.Add("name", typeof(string));
        t.Columns.Add("family", typeof(string));
        //add data to table
        t.Rows.Add(1,"hasan","amiri");
        t.Rows.Add(2, "reza", "amiri");
        t.Rows.Add(3, "amin", "neisi");
         
        //bind table to datagridview
        dataGridView1.DataSource = t;
  2. In the click event of the button, do these steps:

    • Define a dataset
      C#
      DataSet ds = new DataSet();
    • Define a datatable
      C#
      DataTable dt = new DataTable();
      dt.Columns.Add("id", typeof(Int16));
      dt.Columns.Add("name", typeof(string));
      dt.Columns.Add("family", typeof(string));
    • Write DataGridView data to the datatable
      C#
      foreach (DataGridViewRow dgv in DataGridView1.Rows)
      {
          dt.Rows.Add(dgv.Cells[0].Value, dgv.Cells[1].Value, dgv.Cells[2]);
      }
    • Add datatable to the dataset and convert to an XML format file
      C#
      ds.Tables.Add(dt);
      ds.WriteXmlSchema("Sample.xml");

    Now run the application once and press button for making the sample.xml file. After running the application, go to the project folder and bin\debug to see the XML file (sample.xml).

  3. Now right click on the project in Solution Explorer and select Add>New Item and select Crystal Report and click OK:

    Image 1

  4. Choose Standard and press OK:

    Image 2

  5. Select ADO.NET(XML):

    Image 3

  6. Choose the sample.xml file path, browse file by going to bin\debug of the project:

    Image 4

  7. Your table will be in the right side of the wizard:

    Image 5

  8. Press the Next button to see the columns of Table1:

    Image 6

    Now select all columns or press the '>>' button of the wizard and at the end press Finish.

  9. Your Crystal Report form design will look like this:

    Image 7

  10. Now we need a CrystalReportViewer component to show the data. Choose CrystalReportViewer from the toolbox and drag it to your form. In the Click event of the button (bottom of code) write this code:
    C#
    //transefer data to crystalreportviewer
    CrystalReport1 cr = new CrystalReport1();
    cr.SetDataSource(ds);
    crystalReportViewer1.ReportSource = cr;
  11. Finally run the application and press the button to see the data in CrystalReportViewer. Now you can print or export data using this component.

    Image 8

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)