Upload
mrdani-poetra-firmansyah
View
217
Download
0
Embed Size (px)
Citation preview
8/13/2019 Report Vb2010
1/56
In the following section you can see , how to create a sample Database and Tables and data for
running Crystal ReportsTutorials . All examples in the VB.NET Crystal Reports Tutorials
are based on the following database . First we have to create a database . Give the database nameas "crystaldb"
Create a DataBase "crystaldb"
In the crystaldb database , create three tables
OrderM aster , OrderDetails , Product.
OrderMaster
OrderM aster_id
OrderM aster_date
OrderMaster_customer
OrderMaster_createduser
OrderDetails
OrderDetails_id
OrderDetails_masterid
OrderDetails_productid
OrderDetails_qty
Product
Product_id
Product_name
Product_price
The following picture shows the relations of each table :
8/13/2019 Report Vb2010
2/56
SQL command for creation tables are follows :
CREATE TABLE [ dbo].[OrderM aster] (
[OrderM aster_id] [i nt] NOT NULL ,
[OrderM aster_date] [datetime] NULL ,
[OrderMaster_customername] [varchar] (50),
[OrderMaster_createduser] [varchar] (50)
) ON [PRIMARY]
CREATE TABLE [dbo].[OrderDetail s] (
[OrderDetails_id] [in t] NOT NULL ,
[OrderDetails_masteri d] [int] NULL ,
[OrderDetails_productid] [ int] NULL ,
[OrderDetails_qty] [i nt] NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[Product] (
[Product_id] [ int] NOT NULL ,
8/13/2019 Report Vb2010
3/56
[Product_name] [varchar] (50) ,
[Product_pri ce] [numeric](18, 0) NULL
) ON [PRIMARY]
Enter data to the tables :
Order Master Table Data
Order Details Table Data
Product Table Data
8/13/2019 Report Vb2010
4/56
Start your first VB.NET Crystal Reports.
All Crystal Reportsprogramming samples in this tutorials is based on the following database
(crystaldb) . Please take a look at the database structure before you start this tutorial - Click hereto seeDatabase Structure.
Open Visual Studio .NET and select a new Visual Basic .NET Project.
Create a new Crystal Reports for Product table from the above database crystalDB. The ProductTable has three fields (Product_id,Product_name,Product_price) and we are showing the whole
table data in the Crystal Reports.
From main menu in Visual Studio select PROJECT-->Add New Item. Then Add New Item
dialogue will appear and select Crystal Reportsfrom the dialogue box.
http://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htm8/13/2019 Report Vb2010
5/56
Select Report type from Crystal Reports gallery.
8/13/2019 Report Vb2010
6/56
Accept the default settings and click OK.
Next step is to select the appropriate connection to your database. Here we are going to selectOLEDB connection for SQL Server
Select OLE DB (ADO) from Create New Connection .
8/13/2019 Report Vb2010
7/56
Select Microsoft OLE DB Provider for SQL Server.
8/13/2019 Report Vb2010
8/56
Next screen is the SQL Server authentication screen . Select your Sql Server name, enter
userid , passwordand select your Database Name . Click next , Then the screen shows OLEDB Property values , leave it as it is , and click finish.
Then you will get your Server name under OLEDB Connectionfrom there select database name
(Crystaldb) and click the tables , then you can see all your tables from your database.
From the tables list select Product table to the right side list .
8/13/2019 Report Vb2010
9/56
Click Next Button
Select all fields from Product table to the right side list .
8/13/2019 Report Vb2010
10/56
Click Finish Button. Then you can see the Crystal Reports designer window . You can arrange
the design according your requirements. Your screen look like the following picture.
8/13/2019 Report Vb2010
11/56
Now the designing part is over and the next step is to call the created Crystal Reports in VB.NET
through Crystal Reports Viewer control.
Select the default form (Form1.vb) you created in VB.NET and drag a button and
CrystalReportViewercontrol to your form.
Select Form's source code view and put the code on top
Imports CrystalDecisions.CrystalReports.Engine
Put the following source code in the button click event
Download Source Code
Print Source Code
Imports CrystalDecisions.CrystalReports.Engine
Public Class Form1Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.ClickDim cryRpt As New ReportDocumentcryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")CrystalReportViewer1.ReportSource = cryRptCrystalReportViewer1.Refresh()
End SubEnd Class
http://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_step_by_step_download.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_step_by_step_download.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_step_by_step_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_step_by_step_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_step_by_step_print.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_step_by_step_download.htm8/13/2019 Report Vb2010
12/56
NOTES:cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
The Crystal Reports is in your project location, there you can see CrystalReport1.rpt. So give
the full path name of report here.
After you run the source code you will get the report like this.
Hope this tutorial help you to create your first Crystal Reports.
8/13/2019 Report Vb2010
13/56
All Crystal Reportsprogramming samples in this tutorials is based on the following database(crystaldb) . Please take a look at the database structure before you start this tutorial - Click here
to seeDatabase Structure.
Here we are going to do is to generate Crystal Reports from multiple tables. Here we have three
table (ordermaster , orderdetails amd product ) and we are generating report from these threetables by connecting each tables with their related fields.
Open Visual Studio .NET and select a new Visual Basic .NET Project.
From main menu in Visual Studio select PROJECT-->Add New Item. Then Add New Itemdialogue will appear and select Crystal Reportsfrom the dialogue box.
http://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htm8/13/2019 Report Vb2010
14/56
Select Report type from Crystal Reports gallery.
8/13/2019 Report Vb2010
15/56
Accept the default settings and click OK.
Next step is to select the appropriate connection to your database. Here we are going to selectOLEDB connection for SQL Server
Select OLE DB (ADO) from Create New Connection .
8/13/2019 Report Vb2010
16/56
Select Microsoft OLE DB Provider for SQL Server.
8/13/2019 Report Vb2010
17/56
Next screen is the SQL Server authentication screen . Select your Sql Server name, enter
userid , passwordand select your Database Name . Click next , Then the screen shows OLEDB Property values , leave it as it is , and click finish.
Then you will get your Server name under OLEDB Connectionfrom there select database name
(Crystaldb) and click the tables , then you can see all your tables from your database.
Select all table from the table list to right side list box, because we are creating report from three
tables ( OrderMaster, OrderDetails, Product) .
8/13/2019 Report Vb2010
18/56
The next step is to make relation between these selected tables. Here we are connecting therelated fields from each table. For that we arrange the tables in visible area in the list (this is not
necessary ) and select the field we are going to make relation and drag to the related field of theother table. After made the relation the screen is look like the following picture .
8/13/2019 Report Vb2010
19/56
Next step is to select the fields from the tables . Here we are selecting only Customername ,
orderdate from ordermastertable , Productname from product table and quantity from order
details.
8/13/2019 Report Vb2010
20/56
Click the Finish button because now we are not using other functionalities of this wizard. After
that you will get the Crystal Reports designer window . You can arrange the fields in the
designer window according to your requirement to view the report . For rearranging you can dragthe field object in the screen . For editing right click the field object and select Edit Text Object.
The following picture shows the sample of designer window after rearrange the field.
8/13/2019 Report Vb2010
21/56
Now the designing part is over and the next step is to call the created Crystal Reports in VB.NET
through Crystal Reports Viewer control.
Select the default form (Form1.vb) you created in VB.NET and drag a button and
CrystalReportViewercontrol to your form.
Select Form's source code view and put the code on top
Imports CrystalDecisions.CrystalReports.Engine
Put the following source code in the button click event
Download Source Code
Print Source Code
Imports CrystalDecisions.CrystalReports.Engine
Public Class Form1Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.ClickDim cryRpt As New ReportDocumentcryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")CrystalReportViewer1.ReportSource = cryRptCrystalReportViewer1.Refresh()
End SubEnd Class
http://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_from_multiple_tables_download.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_from_multiple_tables_download.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_from_multiple_tables_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_from_multiple_tables_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_from_multiple_tables_print.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_from_multiple_tables_download.htm8/13/2019 Report Vb2010
22/56
NOTES:cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
The Crystal Report is in your project location, there you can see CrystalReport1.rpt. So give
the full path name of report here.
After you run the source code you will get the report like this.
8/13/2019 Report Vb2010
23/56
All Crystal Reportsprogramming samples in this tutorials is based on the following database(crystaldb) . Please take a look at the database structure before you start this tutorial - Click here
to seeDatabase Structure.
Here we are passing a String parameter from vb.net to Crystal Reports . For that , from vb.net
program we pass a customer name as parameter and show all the order of that customer to theCrystal Reports.
In the previous tutorial we saw how to generate a Crystal Reports from multiple tables. Here isthe continuation of that tutorial , the only different is that we are passing a Customer Name as a
String parameter and get the report of that particular Customer only . Before starting to this
tutorial just take a look at the previous tutorial (Crystal Report from multiple tables).
In the previous section we are getting the report of all orders from all customers , that is the all
order placed by all customers , here is only one customer.
Hope you went through previous tutorial , if not click here (Crystal Report from multiple tables).
Next step is to create a string parameter in Crystal report.
Select the Field Explorer from CrystalReport Menu.
Then you can see Field Explorer in the Left hand side.
Select Parameter Field from Field Explorer and right Click.
http://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_from_multiple_tables.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_from_multiple_tables.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_from_multiple_tables.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_from_multiple_tables.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_from_multiple_tables.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_from_multiple_tables.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_from_multiple_tables.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_from_multiple_tables.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htm8/13/2019 Report Vb2010
24/56
Fill the appropriate name for Name and Promting text fields
After creating the parameter field , we have to create the selection formula for the CrystalReports .
For creating selection formula , Right click on Crystal Reports designer window , select
REPORT -> SELECTION FORMULA -> RECORD .
8/13/2019 Report Vb2010
25/56
Then you can see the record Selection Formula Editor. This for entering the selection formula.For that you have to select the fields from above fields and make the formula .
First you have to select OrderMaster.OrderMaster_customername from Report Field and selectthe comparison operator and select the parameter field. Double click each field then it will be
selected.
Form the following picture you can understand the selection fields.
After the creation of selection formula close that screen .
Now the designing part is over and the next step is to call the created Crystal Reports in VB.NETthrough Crystal Reports Viewer control.
8/13/2019 Report Vb2010
26/56
Select the default form (Form1.vb) you created in VB.NET and drag a Textbox , button and
CrystalReportViewercontrol to your form.
Select Form's source code view and import the following :
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Put the following source code in the button click event
Download Source Code
Print Source Code
Imports CrystalDecisions.CrystalReports.EngineImports CrystalDecisions.SharedPublic Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.Click
Dim cryRpt As New ReportDocumentcryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
Dim crParameterFieldDefinitions As ParameterFieldDefinitionsDim crParameterFieldDefinition As ParameterFieldDefinitionDim crParameterValues As New ParameterValuesDim crParameterDiscreteValue As New ParameterDiscreteValue
crParameterDiscreteValue.Value = TextBox1.TextcrParameterFieldDefinitions = -
cryRpt.DataDefinition.ParameterFieldscrParameterFieldDefinition = _
http://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_parameter_string_download.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_parameter_string_download.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_parameter_string_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_parameter_string_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_parameter_string_print.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_parameter_string_download.htm8/13/2019 Report Vb2010
27/56
crParameterFieldDefinitions.Item("Customername")crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Clear()crParameterValues.Add(crParameterDiscreteValue)crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
CrystalReportViewer1.ReportSource = cryRptCrystalReportViewer1.Refresh()
End SubEnd Class
NOTES:cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
The Crystal Report is in your project location, there you can see CrystalReport1.rpt. So give
the full path name of report here.
Now you can run the program . Enter a Customer Name(enter any existing customer fromOrdermaster table) and click the button , then your report is look like the following picture.
Here we get the result of the Customer4's order details.
8/13/2019 Report Vb2010
28/56
All Crystal Reportsprogramming samples in this tutorials is based on the following database(crystaldb) . Please take a look at the database structure before you start this tutorial - Click here
to seeDatabase Structure.
In this tutorial we are passing an Integer Parameter to Crystal Reports from VB.NET.
In the previous tutorial , we sawpassing a string parameter to Crystal Reportsfrom VB.NET and
get the result. This is very similar to that tutorial , so here showing only the change information
part only. So please take a look at the complete part ofpassing a string parameter to CrystalReportsfrom VB.NET.
When we pass an Integer parameter , we have to create a new Integer parameter in the Parameter
Fields of Field Explorer. Then we will get the following screen and fill the fields like in the
picture .
After creating the parameter field , we have to create the selection formula for the CrystalReports . For creating selection formula , Right click on Crystal Reports designer window , selectREPORT -> SELECTION FORMULA -> RECORD .
Then you can see the record Selection Formula Editor. This for entering the selection formula.For that you have to select the fields from above lists and make the formula .
Here we made the formula like select the records from Product table whose value is greater thanthe input value. For that first we select the table field that is Product_price from Product tableand then we select the comparison operator and finally we select our Parameter we created
earlier. Double click each field then it will automatically selected
http://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htm8/13/2019 Report Vb2010
29/56
After the creation of selection formula close that screen .
Now the designing part is over and the next step is to call the created Crystal Reports in VB.NETthrough Crystal Reports Viewer control.
Select the default form (Form1.vb) you created in VB.NET and drag a Textbox , button andCrystalReportViewercontrol to your form.
Select Form's source code view and import the following :
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Put the following source code in the button click event
Download Source Code
Print Source Code
Imports CrystalDecisions.CrystalReports.EngineImports CrystalDecisions.SharedPublic Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.Click
Dim cryRpt As New ReportDocumentcryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
http://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_parameter_integer_download.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_parameter_integer_download.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_parameter_integer_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_parameter_integer_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_parameter_integer_print.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_parameter_integer_download.htm8/13/2019 Report Vb2010
30/56
Dim crParameterFieldDefinition As ParameterFieldDefinitionDim crParameterValues As New ParameterValuesDim crParameterDiscreteValue As New ParameterDiscreteValue
crParameterDiscreteValue.Value = Convert.ToInt32(TextBox1.Text)crParameterFieldDefinitions = _
cryRpt.DataDefinition.ParameterFieldscrParameterFieldDefinition = _
crParameterFieldDefinitions.Item("Price")crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Clear()crParameterValues.Add(crParameterDiscreteValue)crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
CrystalReportViewer1.ReportSource = cryRptCrystalReportViewer1.Refresh()
End SubEnd Class
NOTES:cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
The Crystal Report is in your project location, there you can see CrystalReport1.rpt. So give
the full path name of report here.
Now you can run the program . Enter any price , then you can see the report of the Product list
whose price is greater than or equal to the entered price.
Here we got the result of Product list whose price is grater than 50.
8/13/2019 Report Vb2010
31/56
All Crystal Reportsprogramming samples in this tutorials is based on the following database(crystaldb) . Please take a look at the database structure before you start this tutorial - Click here
to seeDatabase Structure.
In this tutorial we are passing a date variable to Crystal Reports from VB.NET.
In the previous tutorials , we sawpassing a string parameter to Crystal Reports,integer
parameter to Crystal reportfrom VB.NET and get the result. This tutorial is very similar to the
above two tutorials , so please take look into the two tutorials before we start this one.
When we pass a Date parameter, we have to create a new date parameter in the Parameter Fieldsof Field Explorer. Then we will get the following screen and fill the fields like in the picture .
After creating the parameter field , we have to create the selection formula for the CrystalReports . For creating selection formula , Right click on Crystal Reports designer window , select
REPORT -> SELECTION FORMULA -> RECORD .
Then you can see the record Selection Formula Editor. This for entering the selection formula.
For that you have to select the fields from above lists and make the formula .
Here we are making the formula like , select all records details from the tables whose order dateis greater than the input date parameter. For doing this you have to select Ordermaster.orderdate ,
comparison operator and parameter date field from selection list of Formula Editor and make the
formula.
http://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_integer.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_integer.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_integer.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_integer.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_integer.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_integer.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_parameter_string.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htm8/13/2019 Report Vb2010
32/56
After the creation of selection formula close that screen .
Now the designing part is over and the next step is to call the created Crystal Reports in VB.NETthrough Crystal Report Viewer control.
Select the default form (Form1.vb) you created in VB.NET and drag a Textbox , button andCrystalReportViewercontrol to your form.
Select Form's source code view and import the following :
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Put the following source code in the button click event
Download Source Code
Print Source Code
Imports CrystalDecisions.CrystalReports.EngineImports CrystalDecisions.SharedPublic Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.Click
Dim cryRpt As New ReportDocumentcryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
Dim crParameterFieldDefinitions As ParameterFieldDefinitions
http://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_parameter_date_download.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_parameter_date_download.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_parameter_date_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_parameter_date_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_parameter_date_print.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_parameter_date_download.htm8/13/2019 Report Vb2010
33/56
Dim crParameterFieldDefinition As ParameterFieldDefinitionDim crParameterValues As New ParameterValuesDim crParameterDiscreteValue As New ParameterDiscreteValue
crParameterDiscreteValue.Value = TextBox1.TextcrParameterFieldDefinitions = _
cryRpt.DataDefinition.ParameterFieldscrParameterFieldDefinition = _
crParameterFieldDefinitions.Item("Orderdate")crParameterValues = crParameterFieldDefinition.CurrentValues
crParameterValues.Clear()crParameterValues.Add(crParameterDiscreteValue)crParameterFieldDefinition.ApplyCurrentValues(crParameterValues)
CrystalReportViewer1.ReportSource = cryRptCrystalReportViewer1.Refresh()
End SubEnd Class
NOTES:cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
The Crystal Report is in your project location, there you can see CrystalReport1.rpt. So give
the full path name of report here.
Now you can run the program . Enter any date , then you can see the report whose order date is
greater than or equal to the entered date.
8/13/2019 Report Vb2010
34/56
Here we got the result of orders whose date is greater than the entered date
8/13/2019 Report Vb2010
35/56
All Crystal Reportsprogramming samples in this tutorials is based on the following database(crystaldb) . Please take a look at the database structure before you start this tutorial - Click here
to seeDatabase Structure.
In this section we are passing User ID , Password , Server Name and Database Name
dynamically to Crystal Reports from vb.net .
SITUATIONS :
In many situations this is useful , for example if you develop a database project in a test server
and later you have to migrate it , in such situations you have to give these informationdynamically to Crystal Reports.
In this program we are using our earlier program and pass the values dynamically to Crystal
Reports. Before we start this section take a look at thestep by step Crystal Reportin VB.NET .
In thestep by step Crystal Reportwe created a report selecting all data from the Product table .There is no chance to change the server on runtime in that case because it is a static report . Herewe are passing Server Name , UserID and Password dynamically to the Crystal Reports.
Here we use Crystal Reports ConnectionInfo .
Dim crConnectionI nfo As New ConnectionInfo, and pass these arguments to ConnectionInfo
Select the default form (Form1.vb) you created in VB.NET and drag a button andCrystalReportViewercontrol to your form.
Select Form's source code view and import the following :
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Put the following source code in the button click event
Download Source Code
Print Source Code
Imports CrystalDecisions.CrystalReports.EngineImports CrystalDecisions.SharedPublic Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.Click
Dim cryRpt As New ReportDocumentDim crtableLogoninfos As New TableLogOnInfosDim crtableLogoninfo As New TableLogOnInfoDim crConnectionInfo As New ConnectionInfoDim CrTables As Tables
http://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_load_dynamically_download.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_load_dynamically_download.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_load_dynamically_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_load_dynamically_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_load_dynamically_print.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_load_dynamically_download.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htm8/13/2019 Report Vb2010
36/56
Dim CrTable As Table
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
With crConnectionInfo.ServerName = "YOUR SERVER NAME".DatabaseName = "YOUR DATABASE NAME".UserID = "YOUR DATABASE USERNAME".Password = "YOUR DATABASE PASSWORD"
End With
CrTables = cryRpt.Database.TablesFor Each CrTable In CrTables
crtableLogoninfo = CrTable.LogOnInfocrtableLogoninfo.ConnectionInfo = crConnectionInfoCrTable.ApplyLogOnInfo(crtableLogoninfo)
Next
CrystalReportViewer1.ReportSource = cryRptCrystalReportViewer1.Refresh()
End SubEnd Class
NOTES:cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
The Crystal Report is in your project location, there you can see CrystalReport1.rpt. So give
the full path name of report here.
You have to replace the source code values of " YOUR SERVER NAME" , " YOUR
DATABASE NAME" , " YOUR DATABASE USERNAME" , " YOUR DATABASE
PASSWORD" with your correct values .
When you run this program you will get the following screen.
8/13/2019 Report Vb2010
37/56
8/13/2019 Report Vb2010
38/56
8/13/2019 Report Vb2010
39/56
All Crystal Reportsprogramming samples in this tutorials is based on the following database(crystaldb) . Please take a look at the database structure before you start this tutorial - Click here
to seeDatabase Structure.
In this tutorial we are adding a Formula Fieldin existing Crystal Reports .
SITUATIONS :
If you have a Crystal Reports with Qty and Price , you need an additional field in your Crystal
Reports for the Total of QTY X PRICE . In this situation you have to use the Formula Fieldin
Crystal Reports.
In this tutorial we are showing the all orders with qty and price and the total of each row , that
means each in each row we are showing the total of qty and price. Before starting this tutorial.
Create a new Crystal Reports with fields CustomerName , Order Date , Product Name and
Product Price . If you do not know how to create this report , just look the previous tutorialCrystal Report from multiple tables. In that report selecting only four fields , here we need onemore field Prodcut->Price .
After you create the Crystal Reports you screen is look like the following picture :
Next is to create the a Formula Fieldfor showing the total of Qty and Price .
Right Click Formula Fieldin the Field Explorer and click New. Then you will get an Input
Message Box , type Total in textbox and click Use Editor
http://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_from_multiple_tables.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_from_multiple_tables.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_from_multiple_tables.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htm8/13/2019 Report Vb2010
40/56
Now you can see Formula Editor screen . Now you can enter which formula you want . Here wewant the result of Qty X Pr ice. For that we select OrderDetails.Qty , the multiplication operator
and Product.Price . Double click each field for selection.
Now you can see Total Under the Formula Field. Drag the field in to the Crystal Reports where
ever you want .
8/13/2019 Report Vb2010
41/56
Now the designing part is over . Select the default form (Form1.vb) you created in VB.NET and
drag a button and CrystalReportViewercontrol to your form.
Select Form's source code view and import the following :
Imports CrystalDecisions.CrystalReports.Engine
Put the following source code in the button click event
Download Source Code
Print Source Code
Imports CrystalDecisions.CrystalReports.EngineImports CrystalDecisions.SharedPublic Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.Click
Dim cryRpt As New ReportDocumentcryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")CrystalReportViewer1.ReportSource = cryRptCrystalReportViewer1.Refresh()
End SubEnd Class
NOTES:
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
The Crystal Report is in your project location, there you can see CrystalReport1.rpt. So give
the full path name of report here.
When you run this program you will get the following screen.
http://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_formula_fields_download.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_formula_fields_download.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_formula_fields_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_formula_fields_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_formula_fields_print.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_formula_fields_download.htm8/13/2019 Report Vb2010
42/56
8/13/2019 Report Vb2010
43/56
All Crystal Reportsprogramming samples in this tutorials is based on the following database(crystaldb) . Please take a look at the database structure before you start this tutorial - Click here
to seeDatabase Structure.
In this tutorial we are taking the sum of field value of Total . This is the continuation of the
previous tutorialCrystal Report Formula Field. So before we start this tutorial , take a look atthe previous tutorialCrystal Report Formula Field.
Here we are taking the grand total of the Total field . The Total field is a Formula field is theresult of qty X price .
In the Crystal Reports designer view right click on the Report Footer , just below the Total field
and select Insert -> Summary .
Then you will get a screen , select the Total from the combo box and Sum from next Combo Box, and summary location Grand Total (Report Footer) . Click Ok button
http://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_formula_fields.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_formula_fields.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_formula_fields.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_formula_fields.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_formula_fields.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_formula_fields.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_formula_fields.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_formula_fields.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htm8/13/2019 Report Vb2010
44/56
Now you can see @Total is just below the Total field in the report Footer.
Now the designing part is over . Select the default form (Form1.vb) you created in VB.NET and
drag a button and CrystalReportViewercontrol to your form.
Select Form's source code view and import the following :
Imports CrystalDecisions.CrystalReports.Engine
Put the following source code in the button click event
Download Source Code
Print Source Code
Imports CrystalDecisions.CrystalReports.Engine
http://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_summary_fields_download.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_summary_fields_download.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_summary_fields_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_summary_fields_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_summary_fields_print.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_summary_fields_download.htm8/13/2019 Report Vb2010
45/56
Imports CrystalDecisions.SharedPublic Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.Click
Dim cryRpt As New ReportDocumentcryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")CrystalReportViewer1.ReportSource = cryRptCrystalReportViewer1.Refresh()
End SubEnd Class
NOTES:cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
The Crystal Report is in your project location, there you can see CrystalReport1.rpt. So give
the full path name of report here.
When you run this program you will get the following screen.
8/13/2019 Report Vb2010
46/56
Exporting from Crystal Reports to PDF format, we are using Crystal ReportssCrExportOptions. Also we have to set PdfRtfWordFormatOptions and
ExportFormatType.PortableDocFormat . In the following example you can see how to export aCrystal Reports as a PDF format file.
All Crystal Reportsprogramming samples in this tutorials is based on the following database(crystaldb) . Please take a look at the database structure before you start this tutorial - Click here
to seeDatabase Structure.
In this tutorial we are using our earlier programstep by step Crystal Reportfor pull data from
database to Crystal Reports . Before we start this section take a look at thestep by step Crystal
Reportin VB.NET .
In thestep by step Crystal Reportwe pull all data from Product table , here now we are exporting
that data to a PDF format file.
Select the default form (Form1.vb) you created in VB.NET and drag two buttons (Button1,Button2 ) and CrystalReportViewercontrol to your form.
Select Form's source code view and import the following :
Imports CrystalDecisions.CrystalReports.Engine
Put the following source code in the button click events
Download Source Code
Print Source Code
Imports CrystalDecisions.CrystalReports.EngineImports CrystalDecisions.SharedPublic Class Form1
Dim cryRpt As New ReportDocument
Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.Click
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")CrystalReportViewer1.ReportSource = cryRptCrystalReportViewer1.Refresh()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.ClickTry
Dim CrExportOptions As ExportOptionsDim CrDiskFileDestinationOptions As New _DiskFileDestinationOptions()Dim CrFormatTypeOptions As New PdfRtfWordFormatOptions()CrDiskFileDestinationOptions.DiskFileName = _
"c:\crystalExport.pdf"CrExportOptions = cryRpt.ExportOptions
http://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_export_pdf_download.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_export_pdf_download.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_export_pdf_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_export_pdf_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_export_pdf_print.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_export_pdf_download.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_sample_database.htm8/13/2019 Report Vb2010
47/56
With CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile.ExportFormatType = ExportFormatType.PortableDocFormat.DestinationOptions = CrDiskFileDestinationOptions.FormatOptions = CrFormatTypeOptions
End WithcryRpt.Export()
Catch ex As ExceptionMsgBox(ex.ToString)
End TryEnd Sub
End Class
NOTES:
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
The Crystal Report is in your project location, there you can see CrystalReport1.rpt. So give
the full path name of report here.
When you run this program you will get the PDF file (crystalExport.pdf) in your computer's C:
8/13/2019 Report Vb2010
48/56
Exporting from Crystal Reports to Excel format, we are using Crystal ReportsCrExportOptions. Also we have to set ExcelF ormatOptions and ExportFormatType.Excel . In
the following example you can see how to export a Crystal Reports as a Excel format file.
All Crystal Reportsprogramming samples in this tutorials is based on the following database
(crystaldb) . Please take a look at the database structure before you start this tutorial - Click hereto see Database Structure .
In this tutorial we are using our earlier programstep by step Crystal Reportpull data fromdatabase to Crystal Reports . Before we start this section take a look at thestep by step Crystal
Reportin VB.NET .
In thestep by step Crystal Reportwe pull all data from Product table , here now we are exporting
that data to a Excel format file.
Select the default form (Form1.vb) you created in VB.NET and drag two buttons (Button1,
Button2 ) and CrystalReportViewercontrol to your form.
Select Form's source code view and import the following :
Imports CrystalDecisions.CrystalReports.Engine
Put the following source code in the button click events
Download Source Code
Print Source Code
Imports CrystalDecisions.CrystalReports.EngineImports CrystalDecisions.SharedPublic Class Form1
Dim cryRpt As New ReportDocument
Private Sub Button1_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button1.Click
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")CrystalReportViewer1.ReportSource = cryRptCrystalReportViewer1.Refresh()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles Button2.Click
TryDim CrExportOptions As ExportOptionsDim CrDiskFileDestinationOptions As New _DiskFileDestinationOptions()Dim CrFormatTypeOptions As New ExcelFormatOptionsCrDiskFileDestinationOptions.DiskFileName = _
"c:\crystalExport.xls"CrExportOptions = cryRpt.ExportOptionsWith CrExportOptions
.ExportDestinationType = ExportDestinationType.DiskFile
http://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_export_excel_download.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_export_excel_download.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_export_excel_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_export_excel_print.htmhttp://vb.net-informations.com/crystal-report/files/print/vb.net_crystal_report_export_excel_print.htmhttp://vb.net-informations.com/crystal-report/files/download/vb.net_crystal_report_export_excel_download.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htmhttp://vb.net-informations.com/crystal-report/vb.net_crystal_report_step_by_step.htm8/13/2019 Report Vb2010
49/56
.ExportFormatType = ExportFormatType.Excel.DestinationOptions = CrDiskFileDestinationOptions.FormatOptions = CrFormatTypeOptions
End WithcryRpt.Export()
Catch ex As ExceptionMsgBox(ex.ToString)
End TryEnd Sub
End Class
NOTES:
cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt")
The Crystal Report is in your project location, there you can see CrystalReport1.rpt. So give
the full path name of report here.
When you run this program you will get the Excel file (crystalExport.xls) in your computer's C:
8/13/2019 Report Vb2010
50/56
The DataGridView control provides a customizable table for displaying data. It gives younumber of properties, methods and events to customize its appearance and behavior.
Unfortunately the DataGridView doesn't have a built in printing functionality . So here we do a
tricky way to print the content of DataGridView . Here we add a PrintDocument object to theproject and handle the PrintPage event which is called every time a new page is to be printed.
Here in the PrintPage event we create a Bitmap Object and draw the DataGridView to the
Bitmap Object.
In order to run this vb.net project you have to drag two buttons ,one for load data and one for
print command, and drag a PrintDocument control on your form . The following picture shows
how to drag PrintDocument Object to your project.
Download Source Code
Print Source Code
Imports System.Data.SqlClientPublic Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click
Dim connectionString As String = "Data Source=.;InitialCatalog=pubs;Integrated Security=True"
Dim sql As String = "SELECT * FROM Authors"Dim connection As New SqlConnection(connectionString)Dim dataadapter As New SqlDataAdapter(sql, connection)Dim ds As New DataSet()connection.Open()dataadapter.Fill(ds, "Authors_table")connection.Close()DataGridView1.DataSource = ds
http://vb.net-informations.com/datagridview/files/download/vb.net_datagridview_printing_download.htmhttp://vb.net-informations.com/datagridview/files/download/vb.net_datagridview_printing_download.htmhttp://vb.net-informations.com/datagridview/files/print/vb.net_datagridview_printing_print.htmhttp://vb.net-informations.com/datagridview/files/print/vb.net_datagridview_printing_print.htmhttp://vb.net-informations.com/datagridview/files/print/vb.net_datagridview_printing_print.htmhttp://vb.net-informations.com/datagridview/files/download/vb.net_datagridview_printing_download.htm8/13/2019 Report Vb2010
51/56
DataGridView1.DataMember = "Authors_table"End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button2.Click
PrintDocument1.Print()End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVale As System.Drawing.Printing.PrintPageEventArgs) HandlesPrintDocument1.PrintPage
Dim bm As New Bitmap(Me.DataGridView1.Width, Me.DataGridView1.Height)DataGridView1.DrawToBitmap(bm, New Rectangle(0, 0,
Me.DataGridView1.Width, Me.DataGridView1.Height))e.Graphics.DrawImage(bm, 0, 0)
End SubEnd Class
8/13/2019 Report Vb2010
52/56
The DataGridView control and its related classes are designed to be a flexible, extensible systemfor displaying and editing tabular data. You can use a DataGridView control to display data with
or without an underlying data source.
The following vb.net source code shows how to Import data from an Excel file to a
DataGridView control .
Download Source Code
Print Source Code
Imports System.Data.SqlClientPublic Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click
Dim MyConnection As System.Data.OleDb.OleDbConnectionDim DtSet As System.Data.DataSetDim MyCommand As System.Data.OleDb.OleDbDataAdapter
MyConnection = NewSystem.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;DataSource='c:\vb.net-informations.xls';Extended Properties=Excel 8.0;")
MyCommand = New System.Data.OleDb.OleDbDataAdapter("select * from[Sheet1$]", MyConnection)
MyCommand.TableMappings.Add("Table", "Net-informations.com")DtSet = New System.Data.DataSetMyCommand.Fill(DtSet)DataGridView1.DataSource = DtSet.Tables(0)MyConnection.Close()
End SubEnd Class
http://vb.net-informations.com/datagridview/files/download/vb.net_datagridview_import_download.htmhttp://vb.net-informations.com/datagridview/files/download/vb.net_datagridview_import_download.htmhttp://vb.net-informations.com/datagridview/files/print/vb.net_datagridview_import_print.htmhttp://vb.net-informations.com/datagridview/files/print/vb.net_datagridview_import_print.htmhttp://vb.net-informations.com/datagridview/files/print/vb.net_datagridview_import_print.htmhttp://vb.net-informations.com/datagridview/files/download/vb.net_datagridview_import_download.htm8/13/2019 Report Vb2010
53/56
The DataGridView control provides a customizable table for displaying data. Displaying data ina tabular format is a task you are likely to perform frequently. The DataGridView control is
highly configurable and extensible, and it provides many properties, methods, and events tocustomize its appearance and behavior.
The following vb.net source code shows how to Export the content of a datagridview to an Excelfile.
Download Source Code
Print Source Code
Imports System.Data.SqlClientImports Excel = Microsoft.Office.Interop.ExcelPublic Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button1.Click
Dim connectionString As String = "Data Source=.;InitialCatalog=pubs;Integrated Security=True"
Dim sql As String = "SELECT * FROM Authors"Dim connection As New SqlConnection(connectionString)Dim dataadapter As New SqlDataAdapter(sql, connection)Dim ds As New DataSet()connection.Open()dataadapter.Fill(ds, "Authors_table")connection.Close()DataGridView1.DataSource = dsDataGridView1.DataMember = "Authors_table"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles Button2.Click
Dim xlApp As Excel.ApplicationDim xlWorkBook As Excel.WorkbookDim xlWorkSheet As Excel.WorksheetDim misValue As Object = System.Reflection.Missing.Value
Dim i As Int16, j As Int16
xlApp = New Excel.ApplicationClassxlWorkBook = xlApp.Workbooks.Add(misValue)xlWorkSheet = xlWorkBook.Sheets("sheet1")
For i = 0 To DataGridView1.RowCount - 2For j = 0 To DataGridView1.ColumnCount - 1
xlWorkSheet.Cells(i + 1, j + 1) = DataGridView1(j,i).Value.ToString()
NextNext
http://vb.net-informations.com/datagridview/files/download/vb.net_datagridview_export_download.htmhttp://vb.net-informations.com/datagridview/files/download/vb.net_datagridview_export_download.htmhttp://vb.net-informations.com/datagridview/files/print/vb.net_datagridview_export_print.htmhttp://vb.net-informations.com/datagridview/files/print/vb.net_datagridview_export_print.htmhttp://vb.net-informations.com/datagridview/files/print/vb.net_datagridview_export_print.htmhttp://vb.net-informations.com/datagridview/files/download/vb.net_datagridview_export_download.htm8/13/2019 Report Vb2010
54/56
xlWorkBook.SaveAs("c:\vb.net-informations.xls",Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,_
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue,misValue, misValue)
xlWorkBook.Close(True, misValue, misValue)xlApp.Quit()
releaseObject(xlWorkSheet)releaseObject(xlWorkBook)releaseObject(xlApp)
MessageBox.Show("Over")End Sub
Private Sub releaseObject(ByVal obj As Object)Try
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)obj = Nothing
Catch ex As Exceptionobj = NothingMessageBox.Show("Exception Occured while releasing object " +
ex.ToString())Finally
GC.Collect()End Try
End Sub
End Class
8/13/2019 Report Vb2010
55/56
The DataGridView class allows customization of cells, rows, columns, and borders through theuse of its properties . If a DataGridView has lot of rows then we can implement paging
functionalities to the DataGridView control. While we implement paging we should know the
boundaries of the pages to enable the paging in the DatagridView.
The following vb.net program provides a way to programmatically implement paging in a
Windows Datagrid View control. Here the DataGridView rows fixed as five rows and other two
buttons are there for implementing paging functionalities.
Download Source Code
Print Source Code
Imports System.Data.SqlClientPublic Class Form1
Dim pagingAdapter As SqlDataAdapterDim pagingDS As DataSetDim scrollVal As Integer
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.ClickDim connectionString As String = "Data Source=.;InitialCatalog=pubs;Integrated Security=True"
Dim sql As String = "SELECT * FROM authors"Dim connection As New SqlConnection(connectionString)pagingAdapter = New SqlDataAdapter(sql, connection)pagingDS = New DataSet()connection.Open()pagingAdapter.Fill(pagingDS, scrollVal, 5, "authors_table")connection.Close()
http://vb.net-informations.com/datagridview/files/download/vb.net_datagridview_paging_download.htmhttp://vb.net-informations.com/datagridview/files/download/vb.net_datagridview_paging_download.htmhttp://vb.net-informations.com/datagridview/files/print/vb.net_datagridview_paging_print.htmhttp://vb.net-informations.com/datagridview/files/print/vb.net_datagridview_paging_print.htmhttp://vb.net-informations.com/datagridview/files/print/vb.net_datagridview_paging_print.htmhttp://vb.net-informations.com/datagridview/files/download/vb.net_datagridview_paging_download.htm8/13/2019 Report Vb2010
56/56
DataGridView1.DataSource = pagingDSDataGridView1.DataMember = "authors_table"
End Sub
Private Sub button2_Click(ByVal sender As System.Object, ByVal e AsSystem.EventArgs) Handles button2.Click
scrollVal = scrollVal - 5If scrollVal 23 Then
scrollVal = 18End If
pagingDS.Clear()pagingAdapter.Fill(pagingDS, scrollVal, 5, "authors_table")
End SubEnd Class