Click here to Skip to main content
15,880,972 members
Articles / Programming Languages / XML
Tip/Trick

DataGridView to Crystal Report in C#

Rate me:
Please Sign up or sign in to vote.
4.65/5 (14 votes)
6 Jun 2013CPOL2 min read 116.9K   6.3K   28   11
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)


Written By
Iran (Islamic Republic of) Iran (Islamic Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionSystem.Runtime.InteropServices.COMException: 'the document could not be opened.' Pin
Member 1500546628-Nov-20 5:45
Member 1500546628-Nov-20 5:45 
QuestionHelp !! Pin
Member 144842946-Jun-19 21:42
Member 144842946-Jun-19 21:42 
QuestionPerfect Pin
Member 1372064211-Mar-18 13:25
Member 1372064211-Mar-18 13:25 
Questionhow to view image in crystal report from column image type in sql server ? Pin
Sraj Muneer13-May-17 2:00
Sraj Muneer13-May-17 2:00 
QuestionVery Good solution Pin
Bhavesh P22-Aug-14 0:00
professionalBhavesh P22-Aug-14 0:00 
QuestionCrystal report viewer Pin
Rizny Mubarak13-Nov-13 20:16
Rizny Mubarak13-Nov-13 20:16 
AnswerRe: Crystal report viewer Pin
stevepacko23-Feb-14 18:23
stevepacko23-Feb-14 18:23 
AnswerRe: Crystal report viewer Pin
mohammad amiri11-Mar-14 4:06
mohammad amiri11-Mar-14 4:06 
GeneralMy vote of 5 Pin
rinkesh sharma30-Oct-13 2:30
rinkesh sharma30-Oct-13 2:30 
Questioneasy reports Pin
robonmatt10-Jun-13 22:21
robonmatt10-Jun-13 22:21 
AnswerRe: easy reports Pin
mohammad amiri11-Jun-13 7:29
mohammad amiri11-Jun-13 7:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.