SETHA SITE INFORMATION

  • Increase font size
  • Default font size
  • Decrease font size

Create Crystal Report from Visual Basic .Net 2008 using Data in DataGridView

E-mail Print PDF
This article will show you how to create crystal report using ADO.Net in Visual Basic .Net 2008.
  • Step 1: Prepare data in DataGridView to be placed on CrystalReport

    With Me.DataGridView1
         .Columns.Add("code", "Code")
         .Columns.Add("name", "Name")
         .Columns.Add("gender", "Gender")
         .Columns.Add("address", "Address")
    
         .EditMode = DataGridViewEditMode.EditProgrammatically
         .AllowUserToAddRows = False
    End With
    
    'add data to datagridview
    'example data in 10 rows
    For i As Integer = 1 To 0
        Me.DataGridView1.Rows.Add()
        With Me.DataGridView1.Rows(i)
             .Cells("code").Value = Format(i + 1, "0000")
             .Cells("name").Value = "Student Name" & i + 1
             .Cells("gender").Value = "M"
             .Cells("address").Value = "PP"
         End With
    Next
    
  • Step 2: Create DataSet and DataTable for Crystal Report
    • Click on Project Menu
    • Click on Add New Item
    • Choose DataSet (Enter DataSet name)
    • Create DataTable in DataSet (Same columns in step 1)
  • Step 3: Create and Design Crystal Report using Data from DataTable in Step 2
    • Click on Project Menu
    • Click on Add New Item
    • Choose Reporting -> Crystal Report
    • Choose Create As Blank Report
    • Right Click on Database Field in Field Explorer of Crystal Report
    • Choose Database Expert
    • Choose Project Data -> ADO.Net DataSets -> (DataSet and DataTable Created in Step 2)
    • OK
    • Drag Each Field in Database Fields into Crystal Report
  • Step 4: Create Form to View Crystal Report using CrystalReportViewer control
    • Create New Form
    • Draw CrystalReportViewer Control on Form
    • Coding
    Dim dt As New DataTable
    '
    With dt
         .Columns.Add("code")
         .Columns.Add("name")
         .Columns.Add("gender")
         .Columns.Add("address")
    End With
    '
    For Each dr As DataGridViewRow In Me.DataGridView1.Rows
         dt.Rows.Add(dr.Cells("code").Value, dr.Cells("name").Value, _
                     dr.Cells("gender").Value, dr.Cells("address").Value)
    Next
    '
    Dim rptDoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
    rptDoc = New CrystalReport1
    rptDoc.SetDataSource(dt)
    '
    Form2.CrystalReportViewer1.ReportSource = rptDoc
    Form2.ShowDialog()
    Form2.Dispose()
    

Download Sample Here
 
See Video Below