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
- 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)
{
DataTable t = new DataTable();
t.Columns.Add("id", typeof(Int16));
t.Columns.Add("name", typeof(string));
t.Columns.Add("family", typeof(string));
t.Rows.Add(1,"hasan","amiri");
t.Rows.Add(2, "reza", "amiri");
t.Rows.Add(3, "amin", "neisi");
dataGridView1.DataSource = t;
- 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).
- Now right click on the project in Solution Explorer and select Add>New Item and select Crystal Report and click OK:
- Choose Standard and press OK:
- Select ADO.NET(XML):
- Choose the sample.xml file path, browse file by going to bin\debug of the project:
- Your table will be in the right side of the wizard:
- 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.
- Your Crystal Report form design will look like this:
- 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:
CrystalReport1 cr = new CrystalReport1();
cr.SetDataSource(ds);
crystalReportViewer1.ReportSource = cr;
- Finally run the application and press the button to see the data in
CrystalReportViewer
. Now you can print or export data using this component.