Create Report
Create report by vb.net and crystal report
1. Create new visual basic project.
2. Add button and CrystalReportViewer to form1.
3. Add new DataSet item.
4. Add DataTable in dataset design page and add data table columns.
- Title, FirstName, LastName, Address
6. On Field Explorer right click at Database Fields and select Database Expert.
7. Select Project Data -> ADO.NET DataSets and add customer table to ReportDataset.8. Design Report (Add report header name txtHeader and report footer name txtFooter).
9. Sample code to show report.
Private Sub btnShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
' Get data to dataset
Dim CustomerData As DataSet = CreateDataSet()
' Create report instance and set data to report data source
Dim rpt As New CustomerReport
rpt.SetDataSource(CustomerData.Tables(0))
' Access crystal report object
' Sample to access textbox object
Dim txtHeader As CrystalDecisions.CrystalReports.Engine.TextObject = rpt.Section2.ReportObjects("txtHeader")
txtHeader.Text = "This is Report Header"
Dim txtFooter As CrystalDecisions.CrystalReports.Engine.TextObject = rpt.Section2.ReportObjects("txtFooter")
txtFooter.Text = "This is Report Footer"
' Show Report
Me.CrystalReportViewer1.ReportSource = rpt
End Sub
Private Function CreateDataSet() As DataSet
Dim ds As New DataSet("ReportDataset")
' Create Table
Dim dt As New DataTable("Customer")
dt.Columns.Add("Title", Type.GetType("System.String"))
dt.Columns.Add("FirstName", Type.GetType("System.String"))
dt.Columns.Add("LastName", Type.GetType("System.String"))
dt.Columns.Add("Address", Type.GetType("System.String"))
' Add DataTable to DataSet
ds.Tables.Add(dt)
' Create new row
Dim dr As DataRow = ds.Tables(0).NewRow
dr("Title") = "Mr"
dr("FirstName") = "Tom"
dr("LastName") = "Brown"
dr("Address") = "Tom Address"
ds.Tables(0).Rows.Add(dr)
dr = ds.Tables(0).NewRow
dr("Title") = "Mr"
dr("FirstName") = "John"
dr("LastName") = "Grary"
dr("Address") = "John Address"
ds.Tables(0).Rows.Add(dr)
dr = ds.Tables(0).NewRow
dr("Title") = "Mr"
dr("FirstName") = "Alex"
dr("LastName") = "Smith"
dr("Address") = "Alex Address"
ds.Tables(0).Rows.Add(dr)
Return ds
End Function
10. Show Report.