Print alternatives with .net framework



For those poor soles such as me not having crystalreports available in vb.net standard...
All of the content here can be found at Microsoft website

Private Sub btnData_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnData.Click

    Dim report As ReportDocument

    ' load employees data from pubs
    Dim dbConn As String = _
     "data source=ineroth;initial catalog=pubs;integrated security=SSPI"
    Dim da As New SqlDataAdapter("SELECT emp_id,fname,lname FROM employee", dbConn)
    Dim ds As New DataSet()
    da.Fill(ds)

    ' initialize report
    report = New ReportDocument()
    With report
      .Title = "Pubs Employees"
      .SubTitleLeft = Now
      .SubTitleRight = "Rockford Lhotka"
      .FooterLeft = "Confidential"
      .Font = New Font("Ariel", 10)

      ' set the data source, using autodiscover to find the columns
      .AutoDiscover = True
      .DataSource = ds

      ' override the column names to be more human readable
      .Columns(0).Name = "Employee id"
      .Columns(1).Name = "First name"
      .Columns(2).Name = "Last name"
    End With

    ' display the report
    Dim dlg As New PrintPreviewDialog()
    dlg.Document = report
    dlg.WindowState = FormWindowState.Maximized
    dlg.ShowDialog()

  End Sub

#End Region

#Region " Report from Objects "

  Private Sub btnObject_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnObject.Click

    Dim report As ReportDocument

    ' load some data
    Dim data As New ArrayList()
    data.Add(New Person("John", "Bloomington", "MN"))
    data.Add(New Person("Mary", "Bloomington", "IL"))
    data.Add(New Person("Aaron", "Minneapolis", "MN"))
    data.Add(New Person("Ben", "San Francisco", "CA"))

    ' initialize report
    report = New ReportDocument()
    With report
      .Title = "Report from Person Objects"
      .SubTitleLeft = Now
      .SubTitleRight = "Rockford Lhotka"
      .FooterLeft = "Confidential"
      .Font = New Font("Ariel", 10)

      ' set the data source, using autodiscover to find the columns
      .AutoDiscover = True
      .DataSource = data
    End With

    ' display the report
    Dim dlg As New PrintPreviewDialog()
    dlg.Document = report
    dlg.WindowState = FormWindowState.Maximized
    dlg.ShowDialog()

  End Sub

#End Region

#Region " Report from Multiple data sources "

  Private WithEvents mMultiple As ReportDocument
  Private mObjects As New ArrayList()

  Private Sub btnMultiple_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles btnMultiple.Click

    ' load authors data from pubs
    Dim dbConn As String = _
     "data source=ineroth;initial catalog=pubs;integrated security=SSPI"
    Dim da As New SqlDataAdapter("SELECT emp_id,fname,lname FROM employee", dbConn)
    Dim ds As New DataSet()
    da.Fill(ds)

    ' load some data into a collection of objects
    mObjects.Add(New Person("John", "Bloomington", "MN"))
    mObjects.Add(New Person("Mary", "Bloomington", "IL"))
    mObjects.Add(New Person("Aaron", "Minneapolis", "MN"))
    mObjects.Add(New Person("Ben", "San Francisco", "CA"))

    ' initialize report to report against DataSet
    mMultiple = New ReportDocument()
    With mMultiple
      .Title = "Multipart Report"
      .SubTitleLeft = Now
      ' use the subtitle to indicate the type of data reported
      .SubTitleRight = "Pubs Employees"
      .FooterLeft = "Confidential"
      .Font = New Font("Ariel", 10)

      ' set the data source, using autodiscover to find the columns
      .AutoDiscover = True
      .DataSource = ds
    End With

    ' display the report
    Dim dlg As New PrintPreviewDialog()
    dlg.Document = mMultiple
    dlg.WindowState = FormWindowState.Maximized
    dlg.ShowDialog()

  End Sub

  Private Sub mMultiple_ReportEnd(ByVal sender As Object, _
      ByVal e As vbReport.ReportPageEventArgs) _
      Handles mMultiple.ReportEnd

    ' if we just reported the DataSet we don't want the 
    ' report to end, so override to also report
    ' the objects
    If TypeOf mMultiple.DataSource Is DataSet Then
      ' indicate there ARE more pages
      e.HasMorePages = True

      ' now set the datasource to our collection
      mMultiple.DataSource = mObjects
      ' change the subtitle to indicate the new type of data
      mMultiple.SubTitleRight = "Person Objects"
    End If

  End Sub




Hosted by www.Geocities.ws

1