Click here to Skip to main content
13,300,331 members (50,679 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked
Posted 6 Jun 2013

DataGridView to Crystal Report in C#

, 6 Jun 2013
Rate this:
Please Sign up or sign in to vote.
this tip discuss about showing datagridview data to crystal report using c#


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.


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:

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

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):
    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(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
      DataSet ds = new DataSet();
    • Define a datatable
      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
      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

    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:

  4. Choose Standard and press OK:

  5. Select ADO.NET(XML):

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

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

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

    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:

  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:
    //transefer data to crystalreportviewer
    CrystalReport1 cr = new CrystalReport1();
    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.


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


About the Author

mohammad amiri
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...

Comments and Discussions

Questionhow to view image in crystal report from column image type in sql server ? Pin
Sraj Muneer13-May-17 3:00
memberSraj Muneer13-May-17 3:00 
QuestionVery Good solution Pin
Member 1093195522-Aug-14 1:00
memberMember 1093195522-Aug-14 1:00 
QuestionCrystal report viewer Pin
Rizny Mubarak13-Nov-13 21:16
memberRizny Mubarak13-Nov-13 21:16 
AnswerRe: Crystal report viewer Pin
stevepacko23-Feb-14 19:23
memberstevepacko23-Feb-14 19:23 
AnswerRe: Crystal report viewer Pin
mohammad amiri11-Mar-14 5:06
membermohammad amiri11-Mar-14 5:06 
GeneralMy vote of 5 Pin
rinkesh sharma30-Oct-13 3:30
memberrinkesh sharma30-Oct-13 3:30 
Questioneasy reports Pin
robonmatt10-Jun-13 23:21
memberrobonmatt10-Jun-13 23:21 
AnswerRe: easy reports Pin
mohammad amiri11-Jun-13 8:29
membermohammad amiri11-Jun-13 8: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 6 Jun 2013
Article Copyright 2013 by mohammad amiri
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid