Last post Jul 12, 2012 12:26 PM by KyleMit
Aug 28, 2008 02:21 PM|StephanieD|LINK
I have a Crystal Report that pulls data directly from a database.
I want to switch the Crystal Report to pulling data from a DataView instead.
I have 10 lines of code elsewhere in my project that creates the DataView I want.
I would love to just cut and paste this code somewhere so the DataView will be created and the Crystal Report's data source can be set to it.
My question is.... where should this DataView code live? Does it normally live on the page that contains the CrystalReportViewer? If so, how does it reach over to the CrystalReport.rpt page to set its source property?
Sorry for being so brain-dead! I have searched for a couple hours before posting this question. There is not as much information on the web for Crystal Reports as there is for other .net programming tasks!
Aug 29, 2008 10:01 AM|StephanieD|LINK
After 10 hours of searching, I found a solution that I am not happy with but at least its working.
I will post it here for the benefit of any one else who stumbles across this thread.
Since I am an extreme newbie, this information will seem ridiculously simple to 95% of the people who read it. I'm sure it is a very poor way to do it but at least it works and gave me a working model to start building and improving on.
Anyhoo, my solution works like this:
(1) Use Visual Studio interface to create a Crystal Report file. (Right-click the project name, Add New Item, choose Crystal Report, etc). Go through the wizard if you want, and configure it pull some data in directly from a database. Once the report
is finished, find it in the file system and make another copy of it using a new name (Fubar.rpt, for example). Then go back into Visual Studio and delete it from your project in Solution Explorer. It will remove it from your project and delete the rpt file
itself, but the copy you made will still be there.
(2) Create a new blank page in your project.
(3) In the code-behind on that page, write the code that creates your DataView.
(4) In this new page, at the very top, paste the following. The last three are not necessary if you're not going to have to export your report in formats like Excel. (I am leaving them there in case I want to export to Excel in the future - I would rather
slow my app down by 3 milliseconds that have to spend 2 hours searching for those 3 lines some day.)
(5) Drag a CrystalReportViewer onto this new page. I couldn't find the code to create the report viewer programatically so I had to resort to dragging a blank report viewer onto the page. I think I read somewhere that you can't create the
viewer programatically in the version of CR that ships with VS, but I could be mistaken about that.
(6) Add something like this to your code-behind:
Even though your report was originally created with a link to pull data from a database directly, the rpt.SetDataSource switches it to a different source.
That's it. I hope this helps another Newbie like myself. Do I get points if I click on my own reply as the Answer? >:-)
Jul 12, 2012 12:26 PM|KyleMit|LINK
It wasn't previously possible to set a dataview as the data source for Crystal Reports, but now is in Version XI
Here's some basic sample code to use, exactly the way you would with a dataset or datatable:
Dim crRptDoc As rptName
Dim dt As DataTable
Dim dv As DataView
Dim daPL As SqlDataAdapter
dtFill = New DataTable
'sort with dataview
dv = New DataView(dt)
dv.Sort = "ColName [ASC|DSC]"
'set report data source
crRptDoc = New rptName
The following was excerpted from this article: http://www.code-magazine.com/article.aspx?quickid=0701031&page=6
Additionally, if I’m using Crystal Reports XI, I can now pass a DataView object. Prior versions of Crystal Reports did not support the DataView object. So if I’m using anything prior to Crystal Reports XI and want to push the contents of a DataView
into a report, I have two choices:
If I’m using Visual Studio 2005, I can use the new ADO.NET method, DataView.ToTable, to convert the view to a table.