Data Connectivity
 Crystal Reports Forum : Crystal Reports for Visual Studio 2005 and Newer : Data Connectivity
Message Icon Topic: CR and SetDataSource Post Reply Post New Topic
Author Message
snufse
Newbie
Newbie


Joined: 19 Mar 2008
Location: United States
Online Status: Offline
Posts: 36
Quote snufse Replybullet Topic: CR and SetDataSource
    Posted: 03 Oct 2008 at 10:43am

I have as ASP application where I use a DataSet to display a GridView with a click button to display the report. It works fine

Dim mySqlDataSet As New Data.DataSet
mySqlDataSet = Session("MySqlDataSet")
myCrystalReport.Load(Server.MapPath("CrystalReport1.rpt"))
myCrystalReport.SetDataSource(mySqlDataSet)
CrystalReportViewer1.ReportSource = myCrystalReport
 
On the user interface I allow user to sort columns in the GridView. This bring me to my question: I would like to show the report the way the GridView is sorted. This means that I would like to use the GridView as data source for the report. What options do I have? Thank you.
IP IP Logged
BrianBischof
Admin Group
Admin Group
Avatar

Joined: 09 Nov 2006
Online Status: Offline
Posts: 2458
Quote BrianBischof Replybullet Posted: 03 Oct 2008 at 10:55am
You can get the datasource from the GridView (I forget which property gives you this) and pass it to Crystal Reports. But when you do this, I don't know if it is going to pass you the sorted version or not. It depends on how the GridView performs the sort internally. It might do it in memory and display it, or it might actually rebuild the underlying data source in the sorted order. If it does the latter, then CR would be able to report on it in the sorted format. I haven't done this myself, and it would be helpful if you could experiment with it and let us know how it goes.
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>
IP IP Logged
snufse
Newbie
Newbie


Joined: 19 Mar 2008
Location: United States
Online Status: Offline
Posts: 36
Quote snufse Replybullet Posted: 03 Oct 2008 at 12:15pm
Following will use the GridView as data source (instead of a DataSet) and display the report 
 
Dim GridView1 As New GridView
GridView1.DataSource = Session("myGridView")
myCrystalReport.Load(Server.MapPath("CrystalReport1.rpt"))
myCrystalReport.SetDataSource(GridView1.DataSource)
CrystalReportViewer1.ReportSource = myCrystalReport
 
Now:
 
The initial display of the DataGrid. Save the GridView.DataSource in a Session variable. This will save the sort order to be used in CR.
 
Public Sub DisplayGridView()
Try
 GridView1.DataSource = mySqlDataSet.Tables("JobListTable")
 GridView1.DataBind()
 Session("myGridView") = GridView1.DataSource
 initialSort = False
Catch ex As Exception
 ErrorString = ex.ToString
 ASPNET_MsgBox("An error has occurred displaying the data: " + vbCrLf + ErrorString)
 End Try
End Sub
 
When sorting grid column(s) and you want CR to display the same sort order:
 
Protected Sub gridView1_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)
 GridViewSortExpression = e.SortExpression
 GridView1.DataSource = mySqlDataSet.Tables("JobListTable")
 Dim pageIndex As Integer = GridView1.PageIndex
 GridView1.DataSource = SortDataTable(GridView1.DataSource, False)
 GridView1.DataBind()
 GridView1.PageIndex = pageIndex
 Session("myGridView") = GridView1.DataSource
End Sub


Edited by snufse - 03 Oct 2008 at 12:35pm
IP IP Logged
BrianBischof
Admin Group
Admin Group
Avatar

Joined: 09 Nov 2006
Online Status: Offline
Posts: 2458
Quote BrianBischof Replybullet Posted: 03 Oct 2008 at 12:39pm
Great post. Thanks!
Please support the forum! Tell others by linking to it on your blog or website:<a href="http://www.crystalreportsbook.com/forum/">Crystal Reports Forum</a>
IP IP Logged
Post Reply Post New Topic
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum



This page was generated in 0.047 seconds.