Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Export Datagridview to Crystal Report in C# Windows Application VB.NET

Dec 10, 2014
Introduction:

Here I will explain how to export datagridview data to
crystal reports in c#, vb.net in windows application using visual studio 2010 or send datagridview to crystal reports with example in c#, vb.net windows form or application.

Description:

In Previous posts I explained
how to install crystal reports in visual studio 2010, create crystal report using oledb connection in asp.net, crystal reports example in asp.net with dataset or datatable,  and many articles related to crystal reports, asp.net, c#, vb.net . Now I will explain how to export datagridview data to crystal reports in c#, vb.net in windows application using visual studio 2010.  


Before start implementation first design new table in your database and give name UserDetails

ColumnName
DataType
UserId
Int(set identity property=true)
UserName
varchar(50)
FirstName
Varchar(50)
LastName
varchar(50)

After completion of table creation enter some dummy data to test application after that create new windows application with following steps

Open visual studio à File à New à select Windows Forms Application à Give Name and Click OK


Now drag and drop DataGridView control from Data section and button controls from toolbox like as shown below


Once we placed controls in our form that will be like as shown below


Now open code behind file and add following namespaces

C# Code


using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
VB.NET


Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
After that add following code in code behind file to bind datagridview in page load and button click event like as shown below

C# Code


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BindData();
}
private void BindData()
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"))
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * from UserDetails", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
}
dataGridView1.DataSource = dt;
}
private void button1_Click(object sender, EventArgs e)
{
DataSet ds=new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("UserId", typeof(Int16));
dt.Columns.Add("UserName", typeof(string));
dt.Columns.Add("FirstName", typeof(string));
dt.Columns.Add("LastName", typeof(string));
foreach (DataGridViewRow dgr in dataGridView1.Rows)
{
dt.Rows.Add(dgr.Cells[0].Value, dgr.Cells[1].Value, dgr.Cells[2].Value, dgr.Cells[3].Value);
}
ds.Tables.Add(dt);
ds.WriteXmlSchema("Sample.xml");
}
}
VB.NET


Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
BindData()
End Sub
Private Sub BindData()
Dim dt As New DataTable()
Using con As New SqlConnection("Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB")
con.Open()
Dim cmd As New SqlCommand("Select * from UserDetails", con)
Dim da As New SqlDataAdapter(cmd)
da.Fill(dt)
con.Close()
End Using
dataGridView1.DataSource = dt
End Sub
Private Sub Button1_Click( sender As System.Object,  e As System.EventArgs) Handles Button1.Click
Dim ds As New DataSet()
Dim dt As New DataTable()
dt.Columns.Add("UserId", GetType(Int16))
dt.Columns.Add("UserName", GetType(String))
dt.Columns.Add("FirstName", GetType(String))
dt.Columns.Add("LastName", GetType(String))
For Each dgr As DataGridViewRow In dataGridView1.Rows
dt.Rows.Add(dgr.Cells(0).Value, dgr.Cells(1).Value, dgr.Cells(2).Value, dgr.Cells(3).Value)
Next
ds.Tables.Add(dt)
ds.WriteXmlSchema("Sample.xml")
End Sub
End Class
If you observe above code we have a “Sample.xml” file this will create database table schema in xml format and it will help us to map table structure to crystal report for that Run your application and click on button it will create “Sample.xml” file to check that one open project folder and go to bin/debug folder to see “Sample.xml” file

Now right click on your application and select Add New item à in that select Crystal Report and click Add

After add crystal report it will prompt Crystal Report Gallery window in that select using the Report wizard option and click OK

Once we add Report Wizard it will open another window Standard Report Creation Wizard to select database connection for crystal report. Here we will select previously created “Sample.xml” file for that select Create new connection à select ADO.NET(XML) like as shown below

Now open ADO.NET (XML) it will prompt new window to select respective file. In that select “Sample.xml” file from project folder path (bin/debug) and click Finish button like as shown below

Once we add our XML file it will create New Dataset with table structure you need to select that table like as shown below and click Next button


Once we select table and press Next button now it will ask you to select required columns to display in crystal report from table based on your requirement like select all columns or only few columns and click Next button like as show below


Now in next windows it will give you option to group columns or Record Selection or other options but all are optional so you can click Next à Next àFinish Once we finished everything it will create crystal report like as shown below

Now open your Form page and drag and drop CrystalReportViewer control from toolbox under Reporting tab once we added CrystalReportViewer control our page will be like as shown below

Now we need to bind data to crystalreport for that add following code in last of button click event  

C# Code


CrystalReport1 cr = new CrystalReport1();
cr.SetDataSource(ds);
crystalReportViewer1.ReportSource = cr;
crystalReportViewer1.Refresh();
VB.NET Code


Dim cr As New CrystalReport1()
cr.SetDataSource(ds)
ReportViewer1.ReportSource = cr
ReportViewer1.Refresh()
Now run your application and click on button to export your datagridview to report and check the output that would be like as shown below

Demo

Download Sample Code Attached




If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

12 comments :

PHdeviloth said...

i can't see the pictures... :(

Anonymous said...

Invalid Argument provided. Failed to open a rowset. Error in File C:\Users\HP\AppData\Local\Temp\gridrepo {C179D44C-0885-4680-B376-F2788D1549D6}.rpt: Invalid argument for database.

Dinesh Soni said...

I didn't understand from where that xml file came ??

Meenakshi said...

I hv made application in which
I need to create bill customer in following pattern

FPan. Firm name. Mobile
Firm Address. Phone
customers name

1 l x l7000 lfromdate l todate
2 l y l 3000 l fromdate l todate
.
.
.

Total 10000

Dt1 has firm and customers details in one row.
and Dt2 has customer's service details multiple rows
dtt and Dt2 is filled by a SP (MSSQL2008)
Plz kindly help

Unknown said...

I couldn't find the file from project file path (Bin/Debug). somebody please help me. from where that "Sample.xml" has came..

Unknown said...
This comment has been removed by the author.
Anonymous said...

help me

Anonymous said...

You have to run the application to get the sample.txt file.

Anonymous said...

Sir I Didn't find that Sample.xml file in my whole project folder...
i run my project around 100 times but i didn't find that file ..
please
Help....

Anonymous said...

hye
If i want to Create Crystal report on the other form what to do?

Unknown said...

not correct

catatanrinne said...

thank you for your tutorial.

now i want to export only selected row from datagridview to crystal report.
could you please help me ?

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.