Crystal Reports Online Training

Learn Online, Anytime, Anywhere

Step-by-step online tutorials.

17.14 DataView Object

DataView Object

The DataView object is useful for taking an existing Dataset object and filtering or sorting its records. You have three options for doing this. The first is to create a new dataset and populate it with the filtered/sorted data. This isn’t a very good idea because the server has to process the request a second time and send the results back to the client. This unnecessarily ties up resources on the server as well as the network. Another option is to change the properties of the report object so that it uses a record selection formula or adds sort fields. The third way is to create a DataView object that is based off the original dataset. This optimizes performance by keeping the server from having to do any additional work as well as saving the network from the additional throughput. Another benefit is that the ADO.NET classes are optimized for doing this type of processing. So you will reap better performance by using a DataView object rather than asking the report to process the same data.

As of when this book was published, there is a bug with Crystal Reports that doesn’t let it print from a DataView object. My technical contact has logged this issue and it will be corrected in a future service pack. This code will be functional once the proper service pack is released.
Listing 17-8. Printing from a DataView object.
Private Sub PrintDataView(myDataSet As DataSet)
Dim myDataView as DataView
Dim myReport As New CrystalReport1()
myDataView = New DataView(myDataSet.Tables("TableName"), _
"CompanyName > 'B'", "", DataViewRowState.CurrentRows)
myReport.SetDataSource(myDataView)
CrystalReportViewer1.ReportSource = myReport
End Sub

The code in Listing 17-8 deviates from the other examples shown so far. All the examples so far are passed a null DataSet object and they populate it so it can be used by the calling method. Since the DataView object needs an existing DataSet object to work, this method is passed a DataSet object that is already populated. You can only create a DataView object after creating a DataSet object elsewhere in your program.

This method creates a DataView object to create a subset of the records, and binds the DataView object to the report using the SetDataSource() method (explained in the next section). Then it prints the report.

In the spirit of never letting a bug defeat me, I wrote a short procedure to convert the DataView object into a new DataTable object. It creates a new DataTable based on the columns in the DataView and then copies the DataView rows into the DataTable. Pass it a DataView object and it returns the corresponding DataTable object. Since this code isn’t specific to Crystal Reports, I’m not going to go into the details of how it works. Just copy and paste the code into your project if you need to use it.

Listing 17-9. Convert the DataView object into a DataTable
'Take a dataview and return a table with the same data
'This is necessary b/c reports can't print views
Private Shared Function ConvertViewToTable(ByVal dv As DataView) As DataTable
Dim Col, Row As Integer
Dim NewRow() As Object
Dim NewTable As New DataTable(dv.Table.TableName)
'Copy the column objects into the table
For Col = 0 To dv.Table.Columns.Count - 1
NewTable.Columns.Add(dv.Table.Columns.Item(Col).ColumnName, dv.Table.Columns.Item(Col).DataType)
Next Col
'Create each row and copy the column data into a new row object
For Row = 0 To dv.Count - 1
ReDim NewRow(dv.Table.Columns.Count - 1)
For Col = 0 To dv.Table.Columns.Count - 1
NewRow(Col) = dv.Item(Row).Item(Col)
Next Col
NewTable.Rows.Add(NewRow)
Next Row
Return NewTable
End Function

As far as performance is concerned, you need to test you own data and compare this to modifying the sorting and filtering with the report object.

Arrays and Collections

A lot of people ask if they can print a report using an array or collection as a data source. Unfortunately, this is not possible. The best option is to copy the array to a dataset and build the report using this dataset.