Click here to Skip to main content
Click here to Skip to main content

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#

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:

<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):
    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
      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
      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:

  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();
    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.

License

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

Comments and Discussions

 
QuestionCrystal report viewer PinmemberRizny Mubarak13-Nov-13 20:16 
AnswerRe: Crystal report viewer Pinmemberstevepacko23-Feb-14 18:23 
AnswerRe: Crystal report viewer Pinmembermohammad amiri11-Mar-14 4:06 
GeneralMy vote of 5 Pinmemberrinkesh sharma30-Oct-13 2:30 
sd
Questioneasy reports Pinmemberrobonmatt10-Jun-13 22:21 
AnswerRe: easy reports Pinmembermohammad amiri11-Jun-13 7:29 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140718.1 | Last Updated 6 Jun 2013
Article Copyright 2013 by mohammad amiri
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid